﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using DAL;
using Entity;
using System.Threading;

namespace Project_V1
{
    public partial class FrmParamRivetSet : Form
    {
        ToolTip tt = new ToolTip();
        private FastenerService objFastenerService = new FastenerService();
        private Fastener objFastener;//DGV初始加载和选择非当前行触发RowEnter事件,创建objFastener对象,刷新数据显示

        private LayerStackService objLayerStackService = new LayerStackService();
        private LayerStack objLayerStack;

        private SettingsService objSettingsService = new SettingsService();
        private Settings objSettings;//DGV初始加载和选择非当前行触发RowEnter事件,创建objSettings对象,刷新数据显示

        public FrmParamRivetSet()
        {
            InitializeComponent();
            this.Load += FrmParamRivetSet_Load;
        }

        private void UpdateText_Insert(Settings objSettings)//TextBox赋值并置为不可编辑
        {



            // this.txt_TechnicsNumber.Text = objSettings.TechnicsNumber.ToString();
            this.txt_MultistageId.Text = objSettings.MultistageId.ToString();
            this.txt_SettingsName.Text = objSettings.FastenerName + "-" + objSettings.Material1 + "_" + objSettings.Thickness1 + "+" + objSettings.Material2 + "_" + objSettings.Thickness2 + "-M" + objSettings.MultistageId.ToString().PadLeft(2, '0');//objSettings.SettingsName;
            this.txt_SettingsId.Text = objSettings.SettingsId.ToString();
            this.txt_Rivet1OKUpPosition.Text = objSettings.TargetVelo1.ToString();
            this.txt_Rivet1OKDownPosition.Text = objSettings.TargetVelo2.ToString();
            this.txt_Rivet1OKSpeedJudgment.Text = objSettings.TargetVelo3.ToString();
            this.txt_Rivet1OKPositionVariation.Text = objSettings.TargetVelo4.ToString();
            this.txt_Rivet1OKPositionVariationSamplintTime.Text = objSettings.TargetVelo5.ToString();
            this.txt_Rivet1OKTorqueJudgment.Text = objSettings.RotationalSpeed1.ToString();
            this.txt_Rivet1OKTorque.Text = objSettings.RotationalSpeed2.ToString();
            this.txt_Rivet1OKPositionJudgment.Text = objSettings.RotationalSpeed3.ToString();
            this.txt_Rivet1OKPosition.Text = objSettings.RotationalSpeed4.ToString();
            this.txt_Rivet1OverTime.Text = objSettings.RotationalSpeed5.ToString();
            this.txt_Rivet1Force.Text = objSettings.TargetPos1.ToString();
            this.txt_Rivet1ForceP.Text = objSettings.TargetPos2.ToString();
            this.txt_Rivet1RotationalSpeed.Text = objSettings.TargetPos3.ToString();
            this.txt_Rivet1RotationalACCTime.Text = objSettings.TargetPos4.ToString();
            this.txt_Rivet1RotationalDECTime.Text = objSettings.TargetPos5.ToString();



            this.txt_TargetDownforce1.Text = objSettings.TargetDownforce1.ToString();
            this.txt_MaxFeedingTime.Text = objSettings.MaxFeedingTime.ToString();
            this.txt_MaxPrePressingTime.Text = objSettings.MaxPrePressingTime.ToString();
            this.txt_MaxRivetingTime.Text = objSettings.MaxRivetingTime.ToString();
            this.txt_UPLimitPos.Text = objSettings.UPLimitPos.ToString();
            this.txt_StopTorque.Text = objSettings.StopTorque.ToString();
            this.txt_Constant_Downforce1.Text = objSettings.Constant_Downforce1.ToString();
            this.txt_PressTime.Text = objSettings.PressTime.ToString();
            this.txt_TargetVelo6.Text = objSettings.TargetVelo6.ToString();
            this.txt_TargetPos6.Text = objSettings.TargetPos6.ToString();
            this.txt_FeedPos1.Text = objSettings.FeedPos1.ToString();
            this.txt_FeedVelo1.Text = objSettings.FeedVelo1.ToString();
            this.txt_FSR_FeedPos1.Text = objSettings.FSR_FeedPos1.ToString();
            this.txt_FeedVelo2.Text = objSettings.FeedVelo2.ToString();
            this.txt_FSR_ContactPos1.Text = objSettings.FSR_ContactPos1.ToString();
            this.txt_UpperPos1.Text = objSettings.UpperPos1.ToString();
            this.txt_LowerPos1.Text = objSettings.LowerPos1.ToString();
            this.txt_StopPressure1.Text = objSettings.StopPressure1.ToString();
            this.txt_Pre_Downforce1.Text = objSettings.Pre_Downforce1.ToString();
            this.txt_PrePressureSetValue.Text = objSettings.PrePressureSetValue.ToString();
            this.txt_FastenerName.Text = objSettings.FastenerName.ToString();
            //this.txt_FastenerId.Text = objSettings.FastenerId.ToString().PadLeft(2,'0');
            this.txt_Para_D.Text = objSettings.Para_D.ToString();
            this.txt_Para_P.Text = objSettings.Para_P.ToString();
            this.txt_Para_H.Text = objSettings.Para_H.ToString();
            this.txt_Para_L.Text = objSettings.Para_L.ToString();
            this.txt_LayerStackName.Text = objSettings.Material1.ToString().Trim() + "_" + objSettings.Thickness1.ToString().Trim() + "+" + objSettings.Material2.ToString().Trim() + "_" + objSettings.Thickness2.ToString().Trim();// objSettings.LayerStackName.ToString();
            //this.txt_LayerStackId.Text = objSettings.LayerStackId.ToString();
            this.txt_Material1.Text = objSettings.Material1.ToString();
            this.txt_Thickness1.Text = objSettings.Thickness1.ToString();
            this.txt_Material2.Text = objSettings.Material2.ToString();
            this.txt_Thickness2.Text = objSettings.Thickness2.ToString();


            this.txt_PrePressHighPosition.Text = objSettings.PrePressHighPosition.ToString();
            this.txt_PrePressHighSpeed.Text = objSettings.PrePressHighSpeed.ToString();
            this.txt_PrePressLowPosition.Text = objSettings.PrePressLowPosition.ToString();
            this.txt_PrePressLowSpeed.Text = objSettings.PrePressLowSpeed.ToString();
            this.txt_PrePressTouchForce.Text = objSettings.PrePressTouchForce.ToString();
            this.txt_PrePressForce.Text = objSettings.PrePressForce.ToString();
            this.txt_Rivet1Force.Text = objSettings.Rivet1Force.ToString();
            this.txt_Rivet1ForceP.Text = objSettings.Rivet1ForceP.ToString();
            this.txt_Rivet1ForceI.Text = objSettings.Rivet1ForceI.ToString();
            this.txt_Rivet1RotationalSpeed.Text = objSettings.Rivet1RotationalSpeed.ToString();
            this.txt_Rivet1RotationalACCTime.Text = objSettings.Rivet1RotationalACCTime.ToString();
            this.txt_Rivet1RotationalDECTime.Text = objSettings.Rivet1RotationalDECTime.ToString();
            this.txt_Rivet1OKUpPosition.Text = objSettings.Rivet1OKUpPosition.ToString();
            this.txt_Rivet1OKDownPosition.Text = objSettings.Rivet1OKDownPosition.ToString();
            this.chk_Rivet1OKSpeedJudgment.Checked = objSettings.Rivet1OKSpeedJudgment;
            this.txt_Rivet1OKSpeed.Text = objSettings.Rivet1OKSpeed.ToString();
            this.chk_Rivet1OKPositionVariationJudgment.Checked = objSettings.Rivet1OKPositionVariationJudgment;
            this.txt_Rivet1OKPositionVariation.Text = objSettings.Rivet1OKPositionVariation.ToString();
            this.txt_Rivet1OKPositionVariationSamplintTime.Text = objSettings.Rivet1OKPositionVariationSamplintTime.ToString();
            this.chk_Rivet1OKTorqueJudgment.Checked = objSettings.Rivet1OKTorqueJudgment;
            this.txt_Rivet1OKTorque.Text = objSettings.Rivet1OKTorque.ToString();
            this.chk_Rivet1OKPositionJudgment.Checked = objSettings.Rivet1OKPositionJudgment;
            this.txt_Rivet1OKPosition.Text = objSettings.Rivet1OKPosition.ToString();
            this.txt_Rivet1OverTime.Text = objSettings.Rivet1OverTime.ToString();
            this.txt_Rivet1MaxSpeed.Text = objSettings.Rivet1MaxSpeed.ToString();
            this.txt_Rivet2Force.Text = objSettings.Rivet2Force.ToString();
            this.txt_Rivet2ForceP.Text = objSettings.Rivet2ForceP.ToString();
            this.txt_Rivet2ForceI.Text = objSettings.Rivet2ForceI.ToString();
            this.txt_Rivet2RotationalSpeed.Text = objSettings.Rivet2RotationalSpeed.ToString();
            this.txt_Rivet2RotationalACCTime.Text = objSettings.Rivet2RotationalACCTime.ToString();
            this.txt_Rivet2RotationalDECTime.Text = objSettings.Rivet2RotationalDECTime.ToString();
            this.txt_Rivet2OKUpPosition.Text = objSettings.Rivet2OKUpPosition.ToString();
            this.txt_Rivet2OKDownPosition.Text = objSettings.Rivet2OKDownPosition.ToString();
            this.chk_Rivet2OKSpeedJudgment.Checked = objSettings.Rivet2OKSpeedJudgment;
            this.txt_Rivet2OKSpeed.Text = objSettings.Rivet2OKSpeed.ToString();
            this.chk_Rivet2OKPositionVariationJudgment.Checked = objSettings.Rivet2OKPositionVariationJudgment;
            this.txt_Rivet2OKPositionVariation.Text = objSettings.Rivet2OKPositionVariation.ToString();
            this.txt_Rivet2OKPositionVariationSamplintTime.Text = objSettings.Rivet2OKPositionVariationSamplintTime.ToString();
            this.chk_Rivet2OKTorqueJudgment.Checked = objSettings.Rivet2OKTorqueJudgment;
            this.txt_Rivet2OKTorque.Text = objSettings.Rivet2OKTorque.ToString();
            this.chk_Rivet2OKPositionJudgment.Checked = objSettings.Rivet2OKPositionJudgment;
            this.txt_Rivet2OKPosition.Text = objSettings.Rivet2OKPosition.ToString();
            this.txt_Rivet2OverTime.Text = objSettings.Rivet2OverTime.ToString();
            this.txt_Rivet2MaxSpeed.Text = objSettings.Rivet2MaxSpeed.ToString();
            this.txt_Rivet3Force.Text = objSettings.Rivet3Force.ToString();
            this.txt_Rivet3ForceP.Text = objSettings.Rivet3ForceP.ToString();
            this.txt_Rivet3ForceI.Text = objSettings.Rivet3ForceI.ToString();
            this.txt_Rivet3RotationalSpeed.Text = objSettings.Rivet3RotationalSpeed.ToString();
            this.txt_Rivet3RotationalACCTime.Text = objSettings.Rivet3RotationalACCTime.ToString();
            this.txt_Rivet3RotationalDECTime.Text = objSettings.Rivet3RotationalDECTime.ToString();
            this.txt_Rivet3OKUpPosition.Text = objSettings.Rivet3OKUpPosition.ToString();
            this.txt_Rivet3OKDownPosition.Text = objSettings.Rivet3OKDownPosition.ToString();
            this.chk_Rivet3OKSpeedJudgment.Checked = objSettings.Rivet3OKSpeedJudgment;
            this.txt_Rivet3OKSpeed.Text = objSettings.Rivet3OKSpeed.ToString();
            this.chk_Rivet3OKPositionVariationJudgment.Checked = objSettings.Rivet3OKPositionVariationJudgment;
            this.txt_Rivet3OKPositionVariation.Text = objSettings.Rivet3OKPositionVariation.ToString();
            this.txt_Rivet3OKPositionVariationSamplintTime.Text = objSettings.Rivet3OKPositionVariationSamplintTime.ToString();
            this.chk_Rivet3OKTorqueJudgment.Checked = objSettings.Rivet3OKTorqueJudgment;
            this.txt_Rivet3OKTorque.Text = objSettings.Rivet3OKTorque.ToString();
            this.chk_Rivet3OKPositionJudgment.Checked = objSettings.Rivet3OKPositionJudgment;
            this.txt_Rivet3OKPosition.Text = objSettings.Rivet3OKPosition.ToString();
            this.txt_Rivet3OverTime.Text = objSettings.Rivet3OverTime.ToString();
            this.txt_Rivet3MaxSpeed.Text = objSettings.Rivet3MaxSpeed.ToString();

            this.txt_Comment.Text = objSettings.Comment;

            //CommonMethods.FastenerList = objFastenerService.GetAll();
            //CommonMethods.LayerStackList = objLayerStackService.GetAll();
            // this.cbo_Fastener.SelectedValue = objSettings.FastenerName.ToString();// + objSettings.FastenerId.ToString();



            //this.txt_Bias1.Text = objSettings.Bias1.ToString();
            //this.txt_TargetDownforce1.Text = objSettings.TargetDownforce1.ToString();
            //this.txt_TargetTorque1.Text = objSettings.TargetTorque1.ToString();
            //this.txt_t1.Text = objSettings.T1.ToString();
            //this.txt_FeedVelo1.Text = objSettings.FeedVelo1.ToString();
            //this.txt_FeedVelo2.Text = objSettings.FeedVelo2.ToString();

            this.cbo_Fastener.DataSource = null;
            this.cbo_Fastener.DisplayMember = "FastenerName";
            this.cbo_Fastener.ValueMember = "FastenerName";
            this.cbo_Fastener.DataSource = CommonMethods.FastenerList;

            this.cbo_LayerStack.DataSource = null;
            this.cbo_LayerStack.DisplayMember = "LayerStackName";
            this.cbo_LayerStack.ValueMember = "LayerStackName";
            this.cbo_LayerStack.DataSource = CommonMethods.LayerStackList;

            this.cbo_Fastener.SelectedValue = objSettings.FastenerName.ToString();// + objSettings.FastenerId.ToString();
            this.cbo_LayerStack.SelectedValue = objSettings.Material1 + "_" + objSettings.Thickness1 + "+" + objSettings.Material2 + "_" + objSettings.Thickness2;
            this.cbo_Fastener.Enabled = false;
            this.cbo_LayerStack.Enabled = false;
            this.txt_SettingsName.Enabled = false;
            this.txt_SettingsId.Enabled = false;

            foreach (TabPage page in tabControl1.TabPages)
            {
                foreach (Control item in page.Controls)
                {
                    if (item is TextBox)
                    {
                        item.Enabled = false;
                    }
                }



                foreach (Control item in page.Controls)
                {
                    if (item is CheckBox)
                    {
                        item.Enabled = false;
                    }
                }
            }
        }
        private void FrmParamRivetSet_Load(object sender, EventArgs e)
        {
            this.cbo_Fastener.DropDownStyle = ComboBoxStyle.DropDownList;//下拉列表不可编辑
            this.cbo_Fastener.DataSource = CommonMethods.FastenerList;
            this.cbo_Fastener.DisplayMember = "FastenerName";
            this.cbo_Fastener.ValueMember = "FastenerName";
            this.cbo_LayerStack.DropDownStyle = ComboBoxStyle.DropDownList;//下拉列表不可编辑
            this.cbo_LayerStack.DataSource = CommonMethods.LayerStackList;
            this.cbo_LayerStack.DisplayMember = "LayerStackName";
            this.cbo_LayerStack.ValueMember = "LayerStackName";

            //CommonMethods.SettingsList = objSettingsService.GetAll();//已经在主界面读取过集合
            this.dgv_data.AutoGenerateColumns = false;//只显示自定义的列
            this.dgv_data.DataSource = CommonMethods.SettingsList;



            foreach (Control item in this.MainPanel.Controls)
            {
                if (item is Button)
                {
                    item.Visible = false;
                }
            }
            this.txt_SettingsName.Enabled = false;
            this.txt_SettingsId.Enabled = false;
            this.cbo_Fastener.Enabled = false;
            this.cbo_LayerStack.Enabled = false;
            //tabControl1.DrawMode = TabDrawMode.OwnerDrawFixed;
            //tabControl1.TabPages["tabPage2"].Visible = false;
            //tabControl1.TabPages["tabPage3"].Visible = false;

            tabControl1.TabPages.Remove(tabPage2);
            tabControl1.TabPages.Remove(tabPage3);
        }

        #region 新增行,需要点击确定\取消
        private void btn_New_Click(object sender, EventArgs e)
        {
            //this.groupBox1.Text = "新增界面";

            this.cbo_Fastener.Enabled = true;
            this.cbo_LayerStack.Enabled = true;
            this.txt_SettingsName.Enabled = true;
            this.txt_SettingsId.Enabled = true;
            this.cbo_Fastener.SelectedIndex = -1;
            this.cbo_LayerStack.SelectedIndex = -1;
            this.txt_SettingsId.Text = "";
            this.txt_SettingsName.Text = "";

            foreach (TabPage page in tabControl1.TabPages)
            {
                foreach (Control item in page.Controls)
                {
                    if (item is TextBox)
                    {
                        item.Enabled = true;
                        //item.Text = "";
                    }
                }


                foreach (Control item in page.Controls)
                {
                    if (item is CheckBox)
                    {
                        item.Enabled = true;
                    }
                }


            }



            this.dgv_data.Enabled = false;//若点击编辑/新增/删除,DGV不能再选定其他行,不能再次编辑/新增/删除,直到点击确定或取消
            this.btn_Delete.Enabled = false;
            this.btn_New.Enabled = false;
            this.btn_Edit.Enabled = false;

            this.btn_SaveNew.Visible = true;
            this.btn_Cancel.Visible = true;
            this.btn_SaveEdit.Visible = false;
        }
        #endregion

        #region 编辑行,需要点击确定\取消,可继续细化文本改变时才可保存
        private void btn_Edit_Click(object sender, EventArgs e)
        {
            //this.groupBox1.Text = "编辑界面";
            if (this.dgv_data.RowCount == 0)
            {
                MessageBox.Show("没有要编辑的行！", "提示信息");
                return;
            }
            if (this.dgv_data.CurrentRow == null)
            {
                MessageBox.Show("请选中要编辑的行！", "提示信息");
                return;
            }

            foreach (TabPage page in tabControl1.TabPages)
            {
                foreach (Control item in page.Controls)
                {
                    if (item is TextBox)
                    {
                        item.Enabled = true;
                    }
                }



                foreach (Control item in page.Controls)
                {
                    if (item is CheckBox)
                    {
                        item.Enabled = true;
                    }
                }
            }
            this.txt_SettingsName.Enabled = true;
            this.txt_SettingsId.Enabled = true;
            this.cbo_Fastener.Enabled = true;
            this.cbo_LayerStack.Enabled = true;

            this.dgv_data.Enabled = false;//若点击编辑/新增/删除,DGV不能再选定其他行,不能再次编辑/新增/删除,直到点击确定或取消
            this.btn_Delete.Enabled = false;
            this.btn_New.Enabled = false;
            this.btn_Edit.Enabled = false;

            this.btn_SaveEdit.Visible = true;
            this.btn_Cancel.Visible = true;
            this.btn_SaveNew.Visible = false;

            this.btn_PressConfirmed.Enabled = true;
            this.btn_StopTorqueConfirmed.Enabled = true;
        }
        #endregion

        #region 删除行,弹出对话框确认是否删除
        private void btn_Delete_Click(object sender, EventArgs e)
        {
            if (this.dgv_data.RowCount == 0)
            {
                MessageBox.Show("没有要删除的行！", "提示信息");
                return;
            }
            if (this.dgv_data.CurrentRow == null)
            {
                MessageBox.Show("请选中要删除的行！", "提示信息");
                return;
            }
            this.dgv_data.Enabled = false;//若点击编辑/新增/删除,DGV不能再选定其他行,不能再次编辑/新增/删除,直到点击确定或取消
            this.btn_Delete.Enabled = false;
            this.btn_New.Enabled = false;
            this.btn_Edit.Enabled = false;

            //弹出对话框,确认是否删除
            string settingsName = this.dgv_data.CurrentRow.Cells["SettingsName"].Value.ToString();
            DialogResult result = MessageBox.Show("确认要删除紧固件  [" + settingsName + "]  吗？", "删除询问", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
            if (result == DialogResult.Cancel)
            {
                this.dgv_data.Enabled = true;
                this.btn_Delete.Enabled = true;
                this.btn_New.Enabled = true;
                this.btn_Edit.Enabled = true;
                return;
            }

            //获取编号并删除
            string settingsId = this.dgv_data.CurrentRow.Cells["SettingsId"].Value.ToString();
            try
            {
                if (objSettingsService.DeleteSettingsById(settingsId) == 1)
                {
                    //btnQuery_Click(null, null);//同步刷新修改的信息
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "提示信息");
            }

            UpdateDGV();

            this.dgv_data.Enabled = true;
            this.btn_Delete.Enabled = true;
            this.btn_New.Enabled = true;
            this.btn_Edit.Enabled = true;
        }
        #endregion

        #region 取消按钮
        private void btn_Cancel_Click(object sender, EventArgs e)
        {
            UpdateText();
            this.dgv_data.Enabled = true;//点击取消
            this.btn_Delete.Enabled = true;
            this.btn_New.Enabled = true;
            this.btn_Edit.Enabled = true;

            this.btn_SaveEdit.Visible = false;
            this.btn_SaveNew.Visible = false;
            this.btn_Cancel.Visible = false;
        }
        #endregion

        //弹出MessageBox(更新成功),编辑需封装EditId
        private void btn_SaveEdit_Click(object sender, EventArgs e)
        {
            #region 数据验证,还需要继续添加,comboBox的选择判断

            //if (this.cbo_Fastener.SelectedIndex == -1)
            //{
            //    MessageBox.Show("请选择紧固件 ! ", "提示信息");
            //    this.cbo_Fastener.Focus();
            //    return;
            //}
            //if (this.cbo_LayerStack.SelectedIndex == -1)
            //{
            //    MessageBox.Show("请选择板材组合 ! ", "提示信息");
            //    this.cbo_LayerStack.Focus();
            //    return;
            //}

            if (this.txt_SettingsName.Text.Trim().Length == 0)
            {
                MessageBox.Show("工艺规范名称不能为空 ! ", "提示信息");
                this.txt_SettingsName.Focus();
                return;
            }
            if (this.txt_SettingsId.Text.Trim().Length == 0)
            {
                MessageBox.Show("工艺规范编号不能为空 ! ", "提示信息");
                this.txt_SettingsId.Focus();
                return;
            }
            //if (this.txt_Rivet1OKUpPosition.Text.Trim().Length == 0)
            //{
            //    MessageBox.Show("设备控制参数不能为空 ! ", "提示信息");
            //    this.txt_Rivet1OKUpPosition.Focus();
            //    return;
            //}
            //if (this.txt_Rivet1OKTorqueJudgment.Text.Trim().Length == 0)
            //{
            //    MessageBox.Show("设备控制参数不能为空 ! ", "提示信息");
            //    this.txt_Rivet1OKTorqueJudgment.Focus();
            //    return;
            //}
            //if (this.txt_Rivet1Force.Text.Trim().Length == 0)
            //{
            //    MessageBox.Show("设备控制参数不能为空 ! ", "提示信息");
            //    this.txt_Rivet1Force.Focus();
            //    return;
            //}

            if (this.txt_PrePressHighPosition.Text.Trim().Length == 0) { MessageBox.Show("预压高速位置参数不能为空 ! ", "提示信息"); this.txt_PrePressHighPosition.Focus(); return; }
            if (this.txt_PrePressHighSpeed.Text.Trim().Length == 0) { MessageBox.Show("预压高速参数不能为空 ! ", "提示信息"); this.txt_PrePressHighSpeed.Focus(); return; }
            if (this.txt_PrePressLowPosition.Text.Trim().Length == 0) { MessageBox.Show("预压低速位置参数不能为空 ! ", "提示信息"); this.txt_PrePressLowPosition.Focus(); return; }
            if (this.txt_PrePressLowSpeed.Text.Trim().Length == 0) { MessageBox.Show("预压低速参数不能为空 ! ", "提示信息"); this.txt_PrePressLowSpeed.Focus(); return; }
            if (this.txt_PrePressTouchForce.Text.Trim().Length == 0) { MessageBox.Show("预压接触力参数不能为空 ! ", "提示信息"); this.txt_PrePressTouchForce.Focus(); return; }
            if (this.txt_PrePressForce.Text.Trim().Length == 0) { MessageBox.Show("预压力参数不能为空 ! ", "提示信息"); this.txt_PrePressForce.Focus(); return; }
            if (this.txt_MaxPrePressingTime.Text.Trim().Length == 0) { MessageBox.Show("预压超时参数不能为空 ! ", "提示信息"); this.txt_MaxPrePressingTime.Focus(); return; }
            if (this.txt_MaxRivetingTime.Text.Trim().Length == 0) { MessageBox.Show("铆接超时(ms)参数不能为空 ! ", "提示信息"); this.txt_MaxRivetingTime.Focus(); return; }
            if (this.txt_Rivet1Force.Text.Trim().Length == 0) { MessageBox.Show("第1段铆接力(KN)参数不能为空 ! ", "提示信息"); this.txt_Rivet1Force.Focus(); return; }
            if (this.txt_Rivet1ForceP.Text.Trim().Length == 0) { MessageBox.Show("第1段铆接力比例增益(P)参数不能为空 ! ", "提示信息"); this.txt_Rivet1ForceP.Focus(); return; }
            if (this.txt_Rivet1ForceI.Text.Trim().Length == 0) { MessageBox.Show("第1段铆接力积分(I)参数不能为空 ! ", "提示信息"); this.txt_Rivet1ForceI.Focus(); return; }
            if (this.txt_Rivet1RotationalSpeed.Text.Trim().Length == 0) { MessageBox.Show("第1段铆接旋转速度(rpm)参数不能为空 ! ", "提示信息"); this.txt_Rivet1RotationalSpeed.Focus(); return; }
            if (this.txt_Rivet1RotationalACCTime.Text.Trim().Length == 0) { MessageBox.Show("第1段铆接旋转加速时间(ms)参数不能为空 ! ", "提示信息"); this.txt_Rivet1RotationalACCTime.Focus(); return; }
            if (this.txt_Rivet1RotationalDECTime.Text.Trim().Length == 0) { MessageBox.Show("第1段铆接旋转减速时间(ms)参数不能为空 ! ", "提示信息"); this.txt_Rivet1RotationalDECTime.Focus(); return; }
            if (this.txt_Rivet1OKUpPosition.Text.Trim().Length == 0) { MessageBox.Show("第1段铆接成功上位置(mm)参数不能为空 ! ", "提示信息"); this.txt_Rivet1OKUpPosition.Focus(); return; }
            if (this.txt_Rivet1OKDownPosition.Text.Trim().Length == 0) { MessageBox.Show("第1段铆接成功下位置(mm)参数不能为空 ! ", "提示信息"); this.txt_Rivet1OKDownPosition.Focus(); return; }
            if (this.txt_Rivet1OKSpeed.Text.Trim().Length == 0) { MessageBox.Show("第1段铆接成功速度(mm/s)参数不能为空 ! ", "提示信息"); this.txt_Rivet1OKSpeed.Focus(); return; }
            if (this.txt_Rivet1OKPositionVariation.Text.Trim().Length == 0) { MessageBox.Show("第1段铆接成功位置变化量(mm)参数不能为空 ! ", "提示信息"); this.txt_Rivet1OKPositionVariation.Focus(); return; }
            if (this.txt_Rivet1OKPositionVariationSamplintTime.Text.Trim().Length == 0) { MessageBox.Show("第1段铆接成功位置变化量采样时间(ms)参数不能为空 ! ", "提示信息"); this.txt_Rivet1OKPositionVariationSamplintTime.Focus(); return; }
            if (this.txt_Rivet1OKTorque.Text.Trim().Length == 0) { MessageBox.Show("第1段铆接成功扭矩(Nm)参数不能为空 ! ", "提示信息"); this.txt_Rivet1OKTorque.Focus(); return; }
            if (this.txt_Rivet1OKPosition.Text.Trim().Length == 0) { MessageBox.Show("第1段铆接成功位置(mm)参数不能为空 ! ", "提示信息"); this.txt_Rivet1OKPosition.Focus(); return; }
            if (this.txt_Rivet1OverTime.Text.Trim().Length == 0) { MessageBox.Show("第1段铆接超时(ms)参数不能为空 ! ", "提示信息"); this.txt_Rivet1OverTime.Focus(); return; }
            if (this.txt_Rivet1MaxSpeed.Text.Trim().Length == 0) { MessageBox.Show("第1段铆接最大速度(mm/s)参数不能为空 ! ", "提示信息"); this.txt_Rivet1MaxSpeed.Focus(); return; }
            if (this.txt_Rivet2Force.Text.Trim().Length == 0) { MessageBox.Show("第2段铆接力(KN)参数不能为空 ! ", "提示信息"); this.txt_Rivet2Force.Focus(); return; }
            if (this.txt_Rivet2ForceP.Text.Trim().Length == 0) { MessageBox.Show("第2段铆接力比例增益(P)参数不能为空 ! ", "提示信息"); this.txt_Rivet2ForceP.Focus(); return; }
            if (this.txt_Rivet2ForceI.Text.Trim().Length == 0) { MessageBox.Show("第2段铆接力积分(I)参数不能为空 ! ", "提示信息"); this.txt_Rivet2ForceI.Focus(); return; }
            if (this.txt_Rivet2RotationalSpeed.Text.Trim().Length == 0) { MessageBox.Show("第2段铆接旋转速度(rpm)参数不能为空 ! ", "提示信息"); this.txt_Rivet2RotationalSpeed.Focus(); return; }
            if (this.txt_Rivet2RotationalACCTime.Text.Trim().Length == 0) { MessageBox.Show("第2段铆接旋转加速时间(ms)参数不能为空 ! ", "提示信息"); this.txt_Rivet2RotationalACCTime.Focus(); return; }
            if (this.txt_Rivet2RotationalDECTime.Text.Trim().Length == 0) { MessageBox.Show("第2段铆接旋转减速时间(ms)参数不能为空 ! ", "提示信息"); this.txt_Rivet2RotationalDECTime.Focus(); return; }
            if (this.txt_Rivet2OKUpPosition.Text.Trim().Length == 0) { MessageBox.Show("第2段铆接成功上位置(mm)参数不能为空 ! ", "提示信息"); this.txt_Rivet2OKUpPosition.Focus(); return; }
            if (this.txt_Rivet2OKDownPosition.Text.Trim().Length == 0) { MessageBox.Show("第2段铆接成功下位置(mm)参数不能为空 ! ", "提示信息"); this.txt_Rivet2OKDownPosition.Focus(); return; }
            if (this.txt_Rivet2OKSpeed.Text.Trim().Length == 0) { MessageBox.Show("第2段铆接成功速度(mm/s)参数不能为空 ! ", "提示信息"); this.txt_Rivet2OKSpeed.Focus(); return; }
            if (this.txt_Rivet2OKPositionVariation.Text.Trim().Length == 0) { MessageBox.Show("第2段铆接成功位置变化量(mm)参数不能为空 ! ", "提示信息"); this.txt_Rivet2OKPositionVariation.Focus(); return; }
            if (this.txt_Rivet2OKPositionVariationSamplintTime.Text.Trim().Length == 0) { MessageBox.Show("第2段铆接成功位置变化量采样时间(ms)参数不能为空 ! ", "提示信息"); this.txt_Rivet2OKPositionVariationSamplintTime.Focus(); return; }
            if (this.txt_Rivet2OKTorque.Text.Trim().Length == 0) { MessageBox.Show("第2段铆接成功扭矩(Nm)参数不能为空 ! ", "提示信息"); this.txt_Rivet2OKTorque.Focus(); return; }
            if (this.txt_Rivet2OKPosition.Text.Trim().Length == 0) { MessageBox.Show("第2段铆接成功位置(mm)参数不能为空 ! ", "提示信息"); this.txt_Rivet2OKPosition.Focus(); return; }
            if (this.txt_Rivet2OverTime.Text.Trim().Length == 0) { MessageBox.Show("第2段铆接超时(ms)参数不能为空 ! ", "提示信息"); this.txt_Rivet2OverTime.Focus(); return; }
            if (this.txt_Rivet2MaxSpeed.Text.Trim().Length == 0) { MessageBox.Show("第2段铆接最大速度(mm/s)参数不能为空 ! ", "提示信息"); this.txt_Rivet2MaxSpeed.Focus(); return; }
            if (this.txt_Rivet3Force.Text.Trim().Length == 0) { MessageBox.Show("第3段铆接力(KN)参数不能为空 ! ", "提示信息"); this.txt_Rivet3Force.Focus(); return; }
            if (this.txt_Rivet3ForceP.Text.Trim().Length == 0) { MessageBox.Show("第3段铆接力比例增益(P)参数不能为空 ! ", "提示信息"); this.txt_Rivet3ForceP.Focus(); return; }
            if (this.txt_Rivet3ForceI.Text.Trim().Length == 0) { MessageBox.Show("第3段铆接力积分(I)参数不能为空 ! ", "提示信息"); this.txt_Rivet3ForceI.Focus(); return; }
            if (this.txt_Rivet3RotationalSpeed.Text.Trim().Length == 0) { MessageBox.Show("第3段铆接旋转速度(rpm)参数不能为空 ! ", "提示信息"); this.txt_Rivet3RotationalSpeed.Focus(); return; }
            if (this.txt_Rivet3RotationalACCTime.Text.Trim().Length == 0) { MessageBox.Show("第3段铆接旋转加速时间(ms)参数不能为空 ! ", "提示信息"); this.txt_Rivet3RotationalACCTime.Focus(); return; }
            if (this.txt_Rivet3RotationalDECTime.Text.Trim().Length == 0) { MessageBox.Show("第3段铆接旋转减速时间(ms)参数不能为空 ! ", "提示信息"); this.txt_Rivet3RotationalDECTime.Focus(); return; }
            if (this.txt_Rivet3OKUpPosition.Text.Trim().Length == 0) { MessageBox.Show("第3段铆接成功上位置(mm)参数不能为空 ! ", "提示信息"); this.txt_Rivet3OKUpPosition.Focus(); return; }
            if (this.txt_Rivet3OKDownPosition.Text.Trim().Length == 0) { MessageBox.Show("第3段铆接成功下位置(mm)参数不能为空 ! ", "提示信息"); this.txt_Rivet3OKDownPosition.Focus(); return; }
            if (this.txt_Rivet3OKSpeed.Text.Trim().Length == 0) { MessageBox.Show("第3段铆接成功速度(mm/s)参数不能为空 ! ", "提示信息"); this.txt_Rivet3OKSpeed.Focus(); return; }
            if (this.txt_Rivet3OKPositionVariation.Text.Trim().Length == 0) { MessageBox.Show("第3段铆接成功位置变化量(mm)参数不能为空 ! ", "提示信息"); this.txt_Rivet3OKPositionVariation.Focus(); return; }
            if (this.txt_Rivet3OKPositionVariationSamplintTime.Text.Trim().Length == 0) { MessageBox.Show("第3段铆接成功位置变化量采样时间(ms)参数不能为空 ! ", "提示信息"); this.txt_Rivet3OKPositionVariationSamplintTime.Focus(); return; }
            if (this.txt_Rivet3OKTorque.Text.Trim().Length == 0) { MessageBox.Show("第3段铆接成功扭矩(Nm)参数不能为空 ! ", "提示信息"); this.txt_Rivet3OKTorque.Focus(); return; }
            if (this.txt_Rivet3OKPosition.Text.Trim().Length == 0) { MessageBox.Show("第3段铆接成功位置(mm)参数不能为空 ! ", "提示信息"); this.txt_Rivet3OKPosition.Focus(); return; }
            if (this.txt_Rivet3OverTime.Text.Trim().Length == 0) { MessageBox.Show("第3段铆接超时(ms)参数不能为空 ! ", "提示信息"); this.txt_Rivet3OverTime.Focus(); return; }
            if (this.txt_Rivet3MaxSpeed.Text.Trim().Length == 0) { MessageBox.Show("第3段铆接最大速度(mm/s)参数不能为空 ! ", "提示信息"); this.txt_Rivet3MaxSpeed.Focus(); return; }




            //if (this.txt_Bias1.Text.Trim().Length == 0)
            //{
            //    MessageBox.Show("设备控制参数不能为空 ! ", "提示信息");
            //    this.txt_Bias1.Focus();
            //    return;
            //}
            //if (this.txt_TargetDownforce1.Text.Trim().Length == 0)
            //{
            //    MessageBox.Show("设备控制参数不能为空 ! ", "提示信息");
            //    this.txt_TargetDownforce1.Focus();
            //    return;
            //}
            //if (this.txt_TargetTorque1.Text.Trim().Length == 0)
            //{
            //    MessageBox.Show("设备控制参数不能为空 ! ", "提示信息");
            //    this.txt_TargetTorque1.Focus();
            //    return;
            //}
            //if (this.txt_t1.Text.Trim().Length == 0)
            //{
            //    MessageBox.Show("设备控制参数不能为空 ! ", "提示信息");
            //    this.txt_t1.Focus();
            //    return;
            //}
            //if (this.txt_FeedVelo1.Text.Trim().Length == 0)
            //{
            //    MessageBox.Show("设备控制参数不能为空 ! ", "提示信息");
            //    this.txt_FeedVelo1.Focus();
            //    return;
            //}
            //if (this.txt_FeedVelo2.Text.Trim().Length == 0)
            //{
            //    MessageBox.Show("设备控制参数不能为空 ! ", "提示信息");
            //    this.txt_FeedVelo2.Focus();
            //    return;
            //}

            //获取选中行的编号
            //this.dgv_data.SelectedRows[0].Cells["SettingsId"].Value
            int a = Convert.ToInt32(this.dgv_data.CurrentRow.Cells["SettingsId"].Value);


            if (this.txt_SettingsId.Text != "")
            { 
                //验证编号是否在数据库内已经存在,并且不等于选中行编号则退出
                if (objSettingsService.IsIdNoExisted(this.txt_SettingsId.Text.Trim()))
                {
                    if (a != Convert.ToInt32(this.txt_SettingsId.Text.Trim()))
                    {
                        MessageBox.Show("工艺规范编号不能和现有编号重复！", "验证提示");
                        this.txt_SettingsId.Focus();
                        this.txt_SettingsId.SelectAll();
                        return;
                    }
                }
            }
            #endregion

            #region 封装工艺规范对象
            Settings objSettings = new Settings();
            objSettings.MultistageId = Convert.ToInt32(this.txt_MultistageId.Text.Trim());
            objSettings.SettingsName = this.txt_SettingsName.Text.Trim();
            objSettings.SettingsId = Convert.ToInt32(this.txt_SettingsId.Text.Trim());
            //objSettings.LayerStackId = Convert.ToInt32(this.cbo_LayerStack.SelectedValue);
            objSettings.MaxPrePressingTime = Convert.ToInt32(this.txt_MaxPrePressingTime.Text.Trim());
            objSettings.MaxRivetingTime = Convert.ToInt32(this.txt_MaxRivetingTime.Text.Trim());
            objSettings.FastenerName = Convert.ToString(this.txt_FastenerName.Text.Trim());
            objSettings.Para_D = Convert.ToString(this.txt_Para_D.Text.Trim());
            objSettings.Para_P = Convert.ToString(this.txt_Para_P.Text.Trim());
            objSettings.Para_H = Convert.ToString(this.txt_Para_H.Text.Trim());
            objSettings.Para_L = Convert.ToString(this.txt_Para_L.Text.Trim());
            objSettings.LayerStackName = this.txt_Material1.Text.Trim() + "_" + this.txt_Material2.Text.Trim() + "+" + this.txt_Thickness1.Text.Trim() + "_" + this.txt_Thickness2.Text.Trim();// 
            objSettings.Material1 = Convert.ToString(this.txt_Material1.Text.Trim());
            objSettings.Thickness1 = Convert.ToString(this.txt_Thickness1.Text.Trim());
            objSettings.Material2 = Convert.ToString(this.txt_Material2.Text.Trim());
            objSettings.Thickness2 = Convert.ToString(this.txt_Thickness2.Text.Trim());
            objSettings.PressConfirmed = (this.btn_PressConfirmed.BackColor == Color.FromArgb(60, 179, 113) ? true : false);//    this.btn_PressConfirmed.Enabled;
            objSettings.StopTorqueConfirmed = (this.btn_StopTorqueConfirmed.BackColor == Color.FromArgb(60, 179, 113) ? true : false); //this.btn_PressConfirmed.Enabled 
            objSettings.PrePressHighPosition = Convert.ToSingle(this.txt_PrePressHighPosition.Text.Trim());
            objSettings.PrePressHighSpeed = Convert.ToSingle(this.txt_PrePressHighSpeed.Text.Trim());
            objSettings.PrePressLowPosition = Convert.ToSingle(this.txt_PrePressLowPosition.Text.Trim());
            objSettings.PrePressLowSpeed = Convert.ToSingle(this.txt_PrePressLowSpeed.Text.Trim());
            objSettings.PrePressTouchForce = Convert.ToSingle(this.txt_PrePressTouchForce.Text.Trim());
            objSettings.PrePressForce = Convert.ToSingle(this.txt_PrePressForce.Text.Trim());
            objSettings.Rivet1Force = Convert.ToSingle(this.txt_Rivet1Force.Text.Trim());
            objSettings.Rivet1ForceP = Convert.ToSingle(this.txt_Rivet1ForceP.Text.Trim());
            objSettings.Rivet1ForceI = Convert.ToSingle(this.txt_Rivet1ForceI.Text.Trim());
            objSettings.Rivet1RotationalSpeed = Convert.ToSingle(this.txt_Rivet1RotationalSpeed.Text.Trim());
            objSettings.Rivet1RotationalACCTime = Convert.ToSingle(this.txt_Rivet1RotationalACCTime.Text.Trim());
            objSettings.Rivet1RotationalDECTime = Convert.ToSingle(this.txt_Rivet1RotationalDECTime.Text.Trim());
            objSettings.Rivet1OKUpPosition = Convert.ToSingle(this.txt_Rivet1OKUpPosition.Text.Trim());
            objSettings.Rivet1OKDownPosition = Convert.ToSingle(this.txt_Rivet1OKDownPosition.Text.Trim());
            objSettings.Rivet1OKSpeedJudgment = this.chk_Rivet1OKSpeedJudgment.Checked;
            objSettings.Rivet1OKSpeed = Convert.ToSingle(this.txt_Rivet1OKSpeed.Text.Trim());
            objSettings.Rivet1OKPositionVariationJudgment = this.chk_Rivet1OKPositionVariationJudgment.Checked;
            objSettings.Rivet1OKPositionVariation = Convert.ToSingle(this.txt_Rivet1OKPositionVariation.Text.Trim());
            objSettings.Rivet1OKPositionVariationSamplintTime = Convert.ToSingle(this.txt_Rivet1OKPositionVariationSamplintTime.Text.Trim());
            objSettings.Rivet1OKTorqueJudgment = this.chk_Rivet1OKTorqueJudgment.Checked;
            objSettings.Rivet1OKTorque = Convert.ToSingle(this.txt_Rivet1OKTorque.Text.Trim());
            objSettings.Rivet1OKPositionJudgment = this.chk_Rivet1OKPositionJudgment.Checked;
            objSettings.Rivet1OKPosition = Convert.ToSingle(this.txt_Rivet1OKPosition.Text.Trim());
            objSettings.Rivet1OverTime = Convert.ToSingle(this.txt_Rivet1OverTime.Text.Trim());
            objSettings.Rivet1MaxSpeed = Convert.ToSingle(this.txt_Rivet1MaxSpeed.Text.Trim());
            objSettings.Rivet2Force = Convert.ToSingle(this.txt_Rivet2Force.Text.Trim());
            objSettings.Rivet2ForceP = Convert.ToSingle(this.txt_Rivet2ForceP.Text.Trim());
            objSettings.Rivet2ForceI = Convert.ToSingle(this.txt_Rivet2ForceI.Text.Trim());
            objSettings.Rivet2RotationalSpeed = Convert.ToSingle(this.txt_Rivet2RotationalSpeed.Text.Trim());
            objSettings.Rivet2RotationalACCTime = Convert.ToSingle(this.txt_Rivet2RotationalACCTime.Text.Trim());
            objSettings.Rivet2RotationalDECTime = Convert.ToSingle(this.txt_Rivet2RotationalDECTime.Text.Trim());
            objSettings.Rivet2OKUpPosition = Convert.ToSingle(this.txt_Rivet2OKUpPosition.Text.Trim());
            objSettings.Rivet2OKDownPosition = Convert.ToSingle(this.txt_Rivet2OKDownPosition.Text.Trim());
            objSettings.Rivet2OKSpeedJudgment = this.chk_Rivet2OKSpeedJudgment.Checked;
            objSettings.Rivet2OKSpeed = Convert.ToSingle(this.txt_Rivet2OKSpeed.Text.Trim());
            objSettings.Rivet2OKPositionVariationJudgment = this.chk_Rivet2OKPositionVariationJudgment.Checked;
            objSettings.Rivet2OKPositionVariation = Convert.ToSingle(this.txt_Rivet2OKPositionVariation.Text.Trim());
            objSettings.Rivet2OKPositionVariationSamplintTime = Convert.ToSingle(this.txt_Rivet2OKPositionVariationSamplintTime.Text.Trim());
            objSettings.Rivet2OKTorqueJudgment = this.chk_Rivet2OKTorqueJudgment.Checked;
            objSettings.Rivet2OKTorque = Convert.ToSingle(this.txt_Rivet2OKTorque.Text.Trim());
            objSettings.Rivet2OKPositionJudgment = this.chk_Rivet2OKPositionJudgment.Checked;
            objSettings.Rivet2OKPosition = Convert.ToSingle(this.txt_Rivet2OKPosition.Text.Trim());
            objSettings.Rivet2OverTime = Convert.ToSingle(this.txt_Rivet2OverTime.Text.Trim());
            objSettings.Rivet2MaxSpeed = Convert.ToSingle(this.txt_Rivet2MaxSpeed.Text.Trim());
            objSettings.Rivet3Force = Convert.ToSingle(this.txt_Rivet3Force.Text.Trim());
            objSettings.Rivet3ForceP = Convert.ToSingle(this.txt_Rivet3ForceP.Text.Trim());
            objSettings.Rivet3ForceI = Convert.ToSingle(this.txt_Rivet3ForceI.Text.Trim());
            objSettings.Rivet3RotationalSpeed = Convert.ToSingle(this.txt_Rivet3RotationalSpeed.Text.Trim());
            objSettings.Rivet3RotationalACCTime = Convert.ToSingle(this.txt_Rivet3RotationalACCTime.Text.Trim());
            objSettings.Rivet3RotationalDECTime = Convert.ToSingle(this.txt_Rivet3RotationalDECTime.Text.Trim());
            objSettings.Rivet3OKUpPosition = Convert.ToSingle(this.txt_Rivet3OKUpPosition.Text.Trim());
            objSettings.Rivet3OKDownPosition = Convert.ToSingle(this.txt_Rivet3OKDownPosition.Text.Trim());
            objSettings.Rivet3OKSpeedJudgment = this.chk_Rivet3OKSpeedJudgment.Checked;
            objSettings.Rivet3OKSpeed = Convert.ToSingle(this.txt_Rivet3OKSpeed.Text.Trim());
            objSettings.Rivet3OKPositionVariationJudgment = this.chk_Rivet3OKPositionVariationJudgment.Checked;
            objSettings.Rivet3OKPositionVariation = Convert.ToSingle(this.txt_Rivet3OKPositionVariation.Text.Trim());
            objSettings.Rivet3OKPositionVariationSamplintTime = Convert.ToSingle(this.txt_Rivet3OKPositionVariationSamplintTime.Text.Trim());
            objSettings.Rivet3OKTorqueJudgment = this.chk_Rivet3OKTorqueJudgment.Checked;
            objSettings.Rivet3OKTorque = Convert.ToSingle(this.txt_Rivet3OKTorque.Text.Trim());
            objSettings.Rivet3OKPositionJudgment = this.chk_Rivet3OKPositionJudgment.Checked;
            objSettings.Rivet3OKPosition = Convert.ToSingle(this.txt_Rivet3OKPosition.Text.Trim());
            objSettings.Rivet3OverTime = Convert.ToSingle(this.txt_Rivet3OverTime.Text.Trim());
            objSettings.Rivet3MaxSpeed = Convert.ToSingle(this.txt_Rivet3MaxSpeed.Text.Trim());

            objSettings.Comment = this.txt_Comment.Text.Trim();



            objSettings.EidtId = a;
            
            #endregion
            //return;

            #region 封装紧固件对象
            Fastener objFastener = new Fastener()
            {
                FastenerName = this.txt_FastenerName.Text.Trim(),// + txt_FastenerId.Text.Trim(),
                //FastenerId = Convert.ToInt32(this.txt_FastenerId.Text.Trim()),
                Para_D = Convert.ToSingle(this.txt_Para_D.Text.Trim()),
                Para_P = Convert.ToSingle(this.txt_Para_P.Text.Trim()),
                Para_H = Convert.ToSingle(this.txt_Para_H.Text.Trim()),
                Para_L = Convert.ToSingle(this.txt_Para_L.Text.Trim()),
            };

            #endregion




            if (objFastenerService.IsFastenerNameExisted(objFastener.FastenerName))  //1存在
            {

                #region 调用后台数据访问方法,将更新后的对象信息存入数据库
                try
                {
                    if (objFastenerService.ModifyNewFastener(objFastener) == 1)
                    {
                        //MessageBox.Show("紧固件参数更新成功", "提示信息");
                    }
                    else
                    {
                        //MessageBox.Show("更新失败", "提示信息");
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
                #endregion

            }
            else
            {

                #region 调用后台数据访问方法,将对象信息存入数据库
                try
                {
                    if (objFastenerService.AddNewFastener(objFastener) == 1)
                    {
                        // MessageBox.Show("新紧固件添加成功", "提示信息");
                    }
                    else
                    {
                        //MessageBox.Show("添加失败", "提示信息");
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
                #endregion
            }










            #region 封装板材组合对象
            LayerStack objLayerStack = new LayerStack()
            {
                LayerStackName = this.txt_Material1.Text.Trim() + "_" + this.txt_Thickness1.Text.Trim() + "+" + this.txt_Material2.Text.Trim() + "_" + this.txt_Thickness2.Text.Trim(),//this.txt_LayerStackName.Text.Trim(),
                //LayerStackId = Convert.ToInt32(this.txt_LayerStackId.Text.Trim()),
                Material1 = Convert.ToString(this.txt_Material1.Text.Trim()),
                Material2 = Convert.ToString(this.txt_Material2.Text.Trim()),
                Thickness1 = Convert.ToSingle(this.txt_Thickness1.Text.Trim()),
                Thickness2 = Convert.ToSingle(this.txt_Thickness2.Text.Trim())
            };
            #endregion

            //验证编号是否在数据库内已经存在
            if (objLayerStackService.IsNewLayerStackNameExisted(objLayerStack.LayerStackName))
            {

                #region 调用后台数据访问方法,将对象信息存入数据库
                try
                {
                    if (objLayerStackService.ModifyNewLayerStackName(objLayerStack) == 1)
                    {
                        //  MessageBox.Show("板材组合参数更新成功", "提示信息");
                    }
                    else
                    {
                        // MessageBox.Show("更新失败", "提示信息");
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
                #endregion
            }
            else
            {
                #region 调用后台数据访问方法,将对象信息存入数据库
                try
                {
                    if (objLayerStackService.AddNewLayerStackName(objLayerStack) == 1)
                    {
                        // MessageBox.Show("新板材组合添加成功", "提示信息");
                    }
                    else
                    {
                        // MessageBox.Show("添加失败", "提示信息");
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
                #endregion
            }


























            #region 调用后台数据访问方法,将对象信息存入数据库
            try
            {
                if (objSettingsService.ModifySettings(objSettings) == 1)
                {
                    MessageBox.Show("工艺规范参数更新成功", "提示信息");
                }
                else
                {
                    MessageBox.Show("更新失败", "提示信息");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            #endregion

            UpdateDGV();

            CommonMethods.FastenerList = objFastenerService.GetAll();
            CommonMethods.LayerStackList = objLayerStackService.GetAll();
            CommonMethods.SettingsList = objSettingsService.GetAll();
            this.cbo_Fastener.SelectedValue = objSettings.FastenerName.ToString();// + objSettings.FastenerId.ToString();

            for (int i = 0; i < dgv_data.Rows.Count; i++)
            {
                dgv_data.Rows[i].Selected = false;
            }

            for (int i = 0; i < dgv_data.Rows.Count; i++)
            {
                if (dgv_data.Rows[i].Cells[0].Value.ToString() == objSettings.SettingsId.ToString())
                {
                    dgv_data.Rows[i].Selected = true;
                    UpdateText_Insert(objSettings);
                }
            }




            this.dgv_data.Enabled = true;//点击确定,编辑完成
            this.btn_Delete.Enabled = true;
            this.btn_New.Enabled = true;
            this.btn_Edit.Enabled = true;

            this.btn_SaveEdit.Visible = false;
            this.btn_SaveNew.Visible = false;
            this.btn_Cancel.Visible = false;
        }

        private void btn_SaveNew_Click(object sender, EventArgs e)
        {
            #region 数据验证,还需要继续添加,comboBox的选择判断

            //if (this.cbo_Fastener.SelectedIndex == -1)
            //{
            //    MessageBox.Show("请选择紧固件 ! ", "提示信息");
            //    this.cbo_Fastener.Focus();
            //    return;
            //}
            //if (this.cbo_LayerStack.SelectedIndex == -1)
            //{
            //    MessageBox.Show("请选择板材组合 ! ", "提示信息");
            //    this.cbo_LayerStack.Focus();
            //    return;
            //}

            //if (this.txt_SettingsName.Text.Trim().Length == 0)
            //{
            //    MessageBox.Show("工艺规范名称不能为空 ! ", "提示信息");
            //    this.txt_SettingsName.Focus();
            //    return;
            //}
            if (this.txt_SettingsId.Text.Trim().Length == 0)
            {
                MessageBox.Show("工艺号不能为空 ! ", "提示信息");
                this.txt_SettingsId.Focus();
                return;
            }
            //if (this.txt_Rivet1OKUpPosition.Text.Trim().Length == 0)
            //{
            //    MessageBox.Show("设备控制参数不能为空 ! ", "提示信息");
            //    this.txt_Rivet1OKUpPosition.Focus();
            //    return;
            //}
            //if (this.txt_Rivet1OKTorqueJudgment.Text.Trim().Length == 0)
            //{
            //    MessageBox.Show("设备控制参数不能为空 ! ", "提示信息");
            //    this.txt_Rivet1OKTorqueJudgment.Focus();
            //    return;
            //}
            //if (this.txt_Rivet1Force.Text.Trim().Length == 0)
            //{
            //    MessageBox.Show("设备控制参数不能为空 ! ", "提示信息");
            //    this.txt_Rivet1Force.Focus();
            //    return;
            //}


            if (this.txt_PrePressHighPosition.Text.Trim().Length == 0) { MessageBox.Show("预压高速位置参数不能为空 ! ", "提示信息"); this.txt_PrePressHighPosition.Focus(); return; }
            if (this.txt_PrePressHighSpeed.Text.Trim().Length == 0) { MessageBox.Show("预压高速参数不能为空 ! ", "提示信息"); this.txt_PrePressHighSpeed.Focus(); return; }
            if (this.txt_PrePressLowPosition.Text.Trim().Length == 0) { MessageBox.Show("预压低速位置参数不能为空 ! ", "提示信息"); this.txt_PrePressLowPosition.Focus(); return; }
            if (this.txt_PrePressLowSpeed.Text.Trim().Length == 0) { MessageBox.Show("预压低速参数不能为空 ! ", "提示信息"); this.txt_PrePressLowSpeed.Focus(); return; }
            if (this.txt_PrePressTouchForce.Text.Trim().Length == 0) { MessageBox.Show("预压接触力参数不能为空 ! ", "提示信息"); this.txt_PrePressTouchForce.Focus(); return; }
            if (this.txt_PrePressForce.Text.Trim().Length == 0) { MessageBox.Show("预压力参数不能为空 ! ", "提示信息"); this.txt_PrePressForce.Focus(); return; }
            if (this.txt_MaxPrePressingTime.Text.Trim().Length == 0) { MessageBox.Show("预压超时参数不能为空 ! ", "提示信息"); this.txt_MaxPrePressingTime.Focus(); return; }
            if (this.txt_MaxRivetingTime.Text.Trim().Length == 0) { MessageBox.Show("铆接超时(ms)参数不能为空 ! ", "提示信息"); this.txt_MaxRivetingTime.Focus(); return; }
            if (this.txt_Rivet1Force.Text.Trim().Length == 0) { MessageBox.Show("第1段铆接力(KN)参数不能为空 ! ", "提示信息"); this.txt_Rivet1Force.Focus(); return; }
            if (this.txt_Rivet1ForceP.Text.Trim().Length == 0) { MessageBox.Show("第1段铆接力比例增益(P)参数不能为空 ! ", "提示信息"); this.txt_Rivet1ForceP.Focus(); return; }
            if (this.txt_Rivet1ForceI.Text.Trim().Length == 0) { MessageBox.Show("第1段铆接力积分(I)参数不能为空 ! ", "提示信息"); this.txt_Rivet1ForceI.Focus(); return; }
            if (this.txt_Rivet1RotationalSpeed.Text.Trim().Length == 0) { MessageBox.Show("第1段铆接旋转速度(rpm)参数不能为空 ! ", "提示信息"); this.txt_Rivet1RotationalSpeed.Focus(); return; }
            if (this.txt_Rivet1RotationalACCTime.Text.Trim().Length == 0) { MessageBox.Show("第1段铆接旋转加速时间(ms)参数不能为空 ! ", "提示信息"); this.txt_Rivet1RotationalACCTime.Focus(); return; }
            if (this.txt_Rivet1RotationalDECTime.Text.Trim().Length == 0) { MessageBox.Show("第1段铆接旋转减速时间(ms)参数不能为空 ! ", "提示信息"); this.txt_Rivet1RotationalDECTime.Focus(); return; }
            if (this.txt_Rivet1OKUpPosition.Text.Trim().Length == 0) { MessageBox.Show("第1段铆接成功上位置(mm)参数不能为空 ! ", "提示信息"); this.txt_Rivet1OKUpPosition.Focus(); return; }
            if (this.txt_Rivet1OKDownPosition.Text.Trim().Length == 0) { MessageBox.Show("第1段铆接成功下位置(mm)参数不能为空 ! ", "提示信息"); this.txt_Rivet1OKDownPosition.Focus(); return; }
            if (this.txt_Rivet1OKSpeed.Text.Trim().Length == 0) { MessageBox.Show("第1段铆接成功速度(mm/s)参数不能为空 ! ", "提示信息"); this.txt_Rivet1OKSpeed.Focus(); return; }
            if (this.txt_Rivet1OKPositionVariation.Text.Trim().Length == 0) { MessageBox.Show("第1段铆接成功位置变化量(mm)参数不能为空 ! ", "提示信息"); this.txt_Rivet1OKPositionVariation.Focus(); return; }
            if (this.txt_Rivet1OKPositionVariationSamplintTime.Text.Trim().Length == 0) { MessageBox.Show("第1段铆接成功位置变化量采样时间(ms)参数不能为空 ! ", "提示信息"); this.txt_Rivet1OKPositionVariationSamplintTime.Focus(); return; }
            if (this.txt_Rivet1OKTorque.Text.Trim().Length == 0) { MessageBox.Show("第1段铆接成功扭矩(Nm)参数不能为空 ! ", "提示信息"); this.txt_Rivet1OKTorque.Focus(); return; }
            if (this.txt_Rivet1OKPosition.Text.Trim().Length == 0) { MessageBox.Show("第1段铆接成功位置(mm)参数不能为空 ! ", "提示信息"); this.txt_Rivet1OKPosition.Focus(); return; }
            if (this.txt_Rivet1OverTime.Text.Trim().Length == 0) { MessageBox.Show("第1段铆接超时(ms)参数不能为空 ! ", "提示信息"); this.txt_Rivet1OverTime.Focus(); return; }
            if (this.txt_Rivet1MaxSpeed.Text.Trim().Length == 0) { MessageBox.Show("第1段铆接最大速度(mm/s)参数不能为空 ! ", "提示信息"); this.txt_Rivet1MaxSpeed.Focus(); return; }
            if (this.txt_Rivet2Force.Text.Trim().Length == 0) { MessageBox.Show("第2段铆接力(KN)参数不能为空 ! ", "提示信息"); this.txt_Rivet2Force.Focus(); return; }
            if (this.txt_Rivet2ForceP.Text.Trim().Length == 0) { MessageBox.Show("第2段铆接力比例增益(P)参数不能为空 ! ", "提示信息"); this.txt_Rivet2ForceP.Focus(); return; }
            if (this.txt_Rivet2ForceI.Text.Trim().Length == 0) { MessageBox.Show("第2段铆接力积分(I)参数不能为空 ! ", "提示信息"); this.txt_Rivet2ForceI.Focus(); return; }
            if (this.txt_Rivet2RotationalSpeed.Text.Trim().Length == 0) { MessageBox.Show("第2段铆接旋转速度(rpm)参数不能为空 ! ", "提示信息"); this.txt_Rivet2RotationalSpeed.Focus(); return; }
            if (this.txt_Rivet2RotationalACCTime.Text.Trim().Length == 0) { MessageBox.Show("第2段铆接旋转加速时间(ms)参数不能为空 ! ", "提示信息"); this.txt_Rivet2RotationalACCTime.Focus(); return; }
            if (this.txt_Rivet2RotationalDECTime.Text.Trim().Length == 0) { MessageBox.Show("第2段铆接旋转减速时间(ms)参数不能为空 ! ", "提示信息"); this.txt_Rivet2RotationalDECTime.Focus(); return; }
            if (this.txt_Rivet2OKUpPosition.Text.Trim().Length == 0) { MessageBox.Show("第2段铆接成功上位置(mm)参数不能为空 ! ", "提示信息"); this.txt_Rivet2OKUpPosition.Focus(); return; }
            if (this.txt_Rivet2OKDownPosition.Text.Trim().Length == 0) { MessageBox.Show("第2段铆接成功下位置(mm)参数不能为空 ! ", "提示信息"); this.txt_Rivet2OKDownPosition.Focus(); return; }
            if (this.txt_Rivet2OKSpeed.Text.Trim().Length == 0) { MessageBox.Show("第2段铆接成功速度(mm/s)参数不能为空 ! ", "提示信息"); this.txt_Rivet2OKSpeed.Focus(); return; }
            if (this.txt_Rivet2OKPositionVariation.Text.Trim().Length == 0) { MessageBox.Show("第2段铆接成功位置变化量(mm)参数不能为空 ! ", "提示信息"); this.txt_Rivet2OKPositionVariation.Focus(); return; }
            if (this.txt_Rivet2OKPositionVariationSamplintTime.Text.Trim().Length == 0) { MessageBox.Show("第2段铆接成功位置变化量采样时间(ms)参数不能为空 ! ", "提示信息"); this.txt_Rivet2OKPositionVariationSamplintTime.Focus(); return; }
            if (this.txt_Rivet2OKTorque.Text.Trim().Length == 0) { MessageBox.Show("第2段铆接成功扭矩(Nm)参数不能为空 ! ", "提示信息"); this.txt_Rivet2OKTorque.Focus(); return; }
            if (this.txt_Rivet2OKPosition.Text.Trim().Length == 0) { MessageBox.Show("第2段铆接成功位置(mm)参数不能为空 ! ", "提示信息"); this.txt_Rivet2OKPosition.Focus(); return; }
            if (this.txt_Rivet2OverTime.Text.Trim().Length == 0) { MessageBox.Show("第2段铆接超时(ms)参数不能为空 ! ", "提示信息"); this.txt_Rivet2OverTime.Focus(); return; }
            if (this.txt_Rivet2MaxSpeed.Text.Trim().Length == 0) { MessageBox.Show("第2段铆接最大速度(mm/s)参数不能为空 ! ", "提示信息"); this.txt_Rivet2MaxSpeed.Focus(); return; }
            if (this.txt_Rivet3Force.Text.Trim().Length == 0) { MessageBox.Show("第3段铆接力(KN)参数不能为空 ! ", "提示信息"); this.txt_Rivet3Force.Focus(); return; }
            if (this.txt_Rivet3ForceP.Text.Trim().Length == 0) { MessageBox.Show("第3段铆接力比例增益(P)参数不能为空 ! ", "提示信息"); this.txt_Rivet3ForceP.Focus(); return; }
            if (this.txt_Rivet3ForceI.Text.Trim().Length == 0) { MessageBox.Show("第3段铆接力积分(I)参数不能为空 ! ", "提示信息"); this.txt_Rivet3ForceI.Focus(); return; }
            if (this.txt_Rivet3RotationalSpeed.Text.Trim().Length == 0) { MessageBox.Show("第3段铆接旋转速度(rpm)参数不能为空 ! ", "提示信息"); this.txt_Rivet3RotationalSpeed.Focus(); return; }
            if (this.txt_Rivet3RotationalACCTime.Text.Trim().Length == 0) { MessageBox.Show("第3段铆接旋转加速时间(ms)参数不能为空 ! ", "提示信息"); this.txt_Rivet3RotationalACCTime.Focus(); return; }
            if (this.txt_Rivet3RotationalDECTime.Text.Trim().Length == 0) { MessageBox.Show("第3段铆接旋转减速时间(ms)参数不能为空 ! ", "提示信息"); this.txt_Rivet3RotationalDECTime.Focus(); return; }
            if (this.txt_Rivet3OKUpPosition.Text.Trim().Length == 0) { MessageBox.Show("第3段铆接成功上位置(mm)参数不能为空 ! ", "提示信息"); this.txt_Rivet3OKUpPosition.Focus(); return; }
            if (this.txt_Rivet3OKDownPosition.Text.Trim().Length == 0) { MessageBox.Show("第3段铆接成功下位置(mm)参数不能为空 ! ", "提示信息"); this.txt_Rivet3OKDownPosition.Focus(); return; }
            if (this.txt_Rivet3OKSpeed.Text.Trim().Length == 0) { MessageBox.Show("第3段铆接成功速度(mm/s)参数不能为空 ! ", "提示信息"); this.txt_Rivet3OKSpeed.Focus(); return; }
            if (this.txt_Rivet3OKPositionVariation.Text.Trim().Length == 0) { MessageBox.Show("第3段铆接成功位置变化量(mm)参数不能为空 ! ", "提示信息"); this.txt_Rivet3OKPositionVariation.Focus(); return; }
            if (this.txt_Rivet3OKPositionVariationSamplintTime.Text.Trim().Length == 0) { MessageBox.Show("第3段铆接成功位置变化量采样时间(ms)参数不能为空 ! ", "提示信息"); this.txt_Rivet3OKPositionVariationSamplintTime.Focus(); return; }
            if (this.txt_Rivet3OKTorque.Text.Trim().Length == 0) { MessageBox.Show("第3段铆接成功扭矩(Nm)参数不能为空 ! ", "提示信息"); this.txt_Rivet3OKTorque.Focus(); return; }
            if (this.txt_Rivet3OKPosition.Text.Trim().Length == 0) { MessageBox.Show("第3段铆接成功位置(mm)参数不能为空 ! ", "提示信息"); this.txt_Rivet3OKPosition.Focus(); return; }
            if (this.txt_Rivet3OverTime.Text.Trim().Length == 0) { MessageBox.Show("第3段铆接超时(ms)参数不能为空 ! ", "提示信息"); this.txt_Rivet3OverTime.Focus(); return; }
            if (this.txt_Rivet3MaxSpeed.Text.Trim().Length == 0) { MessageBox.Show("第3段铆接最大速度(mm/s)参数不能为空 ! ", "提示信息"); this.txt_Rivet3MaxSpeed.Focus(); return; }





            //验证编号是否在数据库内已经存在
            if (this.txt_SettingsId.Text != "" && this.txt_SettingsId.Text != null)
            {
                if (objSettingsService.IsIdNoExisted(this.txt_SettingsId.Text.Trim()))
                {
                    MessageBox.Show("工艺规范编号不能和现有编号重复！", "验证提示");
                    this.txt_SettingsId.Focus();
                    this.txt_SettingsId.SelectAll();
                    return;
                }
            }
            #endregion
            Settings objSettings = new Settings();
             //MessageBox.Show( this.chk_Rivet1OKSpeedJudgment.Checked.ToString());


            try
            {
                #region 封装工艺规范对象
               // objSettings = new Settings()
                  objSettings.MultistageId = Convert.ToInt32(this.txt_MultistageId.Text.Trim());
                    objSettings.SettingsName = this.txt_SettingsName.Text.Trim();
                    objSettings.SettingsId = Convert.ToInt32(this.txt_SettingsId.Text.Trim()); 
                    //objSettings.LayerStackId = Convert.ToInt32(this.cbo_LayerStack.SelectedValue); 
                    objSettings.MaxPrePressingTime = Convert.ToInt32(this.txt_MaxPrePressingTime.Text.Trim());
                    objSettings.MaxRivetingTime = Convert.ToInt32(this.txt_MaxRivetingTime.Text.Trim()); 
                    objSettings.FastenerName = Convert.ToString(this.txt_FastenerName.Text.Trim());
                    objSettings.Para_D = Convert.ToString(this.txt_Para_D.Text.Trim());
                    objSettings.Para_P = Convert.ToString(this.txt_Para_P.Text.Trim());
                    objSettings.Para_H = Convert.ToString(this.txt_Para_H.Text.Trim());
                    objSettings.Para_L = Convert.ToString(this.txt_Para_L.Text.Trim());
                    objSettings.LayerStackName = this.txt_Material1.Text.Trim() + "_" + this.txt_Material2.Text.Trim() + "+" + this.txt_Thickness1.Text.Trim() + "_" + this.txt_Thickness2.Text.Trim();//Convert.ToString(this.txt_LayerStackName.Text.Trim());
                    objSettings.Material1 = Convert.ToString(this.txt_Material1.Text.Trim());
                    objSettings.Thickness1 = Convert.ToString(this.txt_Thickness1.Text.Trim());
                    objSettings.Material2 = Convert.ToString(this.txt_Material2.Text.Trim());
                    objSettings.Thickness2 = Convert.ToString(this.txt_Thickness2.Text.Trim()); 
                    objSettings.PressConfirmed = (this.btn_PressConfirmed.BackColor == Color.FromArgb(60,179,113) ? true : false);//    this.btn_PressConfirmed.Enabled;
                    objSettings.StopTorqueConfirmed = (this.btn_StopTorqueConfirmed.BackColor == Color.FromArgb(60,179,113) ? true : false);  
                    objSettings.PrePressHighPosition = Convert.ToSingle(this.txt_PrePressHighPosition.Text.Trim());
                    objSettings.PrePressHighSpeed = Convert.ToSingle(this.txt_PrePressHighSpeed.Text.Trim());
                    objSettings.PrePressLowPosition = Convert.ToSingle(this.txt_PrePressLowPosition.Text.Trim());
                    objSettings.PrePressLowSpeed = Convert.ToSingle(this.txt_PrePressLowSpeed.Text.Trim());
                    objSettings.PrePressTouchForce = Convert.ToSingle(this.txt_PrePressTouchForce.Text.Trim());
                    objSettings.PrePressForce = Convert.ToSingle(this.txt_PrePressForce.Text.Trim());
                    objSettings.Rivet1Force = Convert.ToSingle(this.txt_Rivet1Force.Text.Trim());
                    objSettings.Rivet1ForceP = Convert.ToSingle(this.txt_Rivet1ForceP.Text.Trim());
                    objSettings.Rivet1ForceI = Convert.ToSingle(this.txt_Rivet1ForceI.Text.Trim());
                    objSettings.Rivet1RotationalSpeed = Convert.ToSingle(this.txt_Rivet1RotationalSpeed.Text.Trim());
                    objSettings.Rivet1RotationalACCTime = Convert.ToSingle(this.txt_Rivet1RotationalACCTime.Text.Trim());
                    objSettings.Rivet1RotationalDECTime = Convert.ToSingle(this.txt_Rivet1RotationalDECTime.Text.Trim());
                    objSettings.Rivet1OKUpPosition = Convert.ToSingle(this.txt_Rivet1OKUpPosition.Text.Trim());
                    objSettings.Rivet1OKDownPosition = Convert.ToSingle(this.txt_Rivet1OKDownPosition.Text.Trim());
                    objSettings.Rivet1OKSpeedJudgment = this.chk_Rivet1OKSpeedJudgment.Checked;
                    objSettings.Rivet1OKSpeed = Convert.ToSingle(this.txt_Rivet1OKSpeed.Text.Trim());
                    objSettings.Rivet1OKPositionVariationJudgment = this.chk_Rivet1OKPositionVariationJudgment.Checked;
                    objSettings.Rivet1OKPositionVariation = Convert.ToSingle(this.txt_Rivet1OKPositionVariation.Text.Trim());
                    objSettings.Rivet1OKPositionVariationSamplintTime = Convert.ToSingle(this.txt_Rivet1OKPositionVariationSamplintTime.Text.Trim());
                    objSettings.Rivet1OKTorqueJudgment = this.chk_Rivet1OKTorqueJudgment.Checked;
                    objSettings.Rivet1OKTorque = Convert.ToSingle(this.txt_Rivet1OKTorque.Text.Trim());
                    objSettings.Rivet1OKPositionJudgment = this.chk_Rivet1OKPositionJudgment.Checked;
                    objSettings.Rivet1OKPosition = Convert.ToSingle(this.txt_Rivet1OKPosition.Text.Trim());
                    objSettings.Rivet1OverTime = Convert.ToSingle(this.txt_Rivet1OverTime.Text.Trim());
                    objSettings.Rivet1MaxSpeed = Convert.ToSingle(this.txt_Rivet1MaxSpeed.Text.Trim());
                    objSettings.Rivet2Force = Convert.ToSingle(this.txt_Rivet2Force.Text.Trim());
                    objSettings.Rivet2ForceP = Convert.ToSingle(this.txt_Rivet2ForceP.Text.Trim());
                    objSettings.Rivet2ForceI = Convert.ToSingle(this.txt_Rivet2ForceI.Text.Trim());
                    objSettings.Rivet2RotationalSpeed = Convert.ToSingle(this.txt_Rivet2RotationalSpeed.Text.Trim());
                    objSettings.Rivet2RotationalACCTime = Convert.ToSingle(this.txt_Rivet2RotationalACCTime.Text.Trim());
                    objSettings.Rivet2RotationalDECTime = Convert.ToSingle(this.txt_Rivet2RotationalDECTime.Text.Trim());
                    objSettings.Rivet2OKUpPosition = Convert.ToSingle(this.txt_Rivet2OKUpPosition.Text.Trim());
                    objSettings.Rivet2OKDownPosition = Convert.ToSingle(this.txt_Rivet2OKDownPosition.Text.Trim());
                    objSettings.Rivet2OKSpeedJudgment = this.chk_Rivet2OKSpeedJudgment.Checked;
                    objSettings.Rivet2OKSpeed = Convert.ToSingle(this.txt_Rivet2OKSpeed.Text.Trim());
                    objSettings.Rivet2OKPositionVariationJudgment = this.chk_Rivet2OKPositionVariationJudgment.Checked;
                    objSettings.Rivet2OKPositionVariation = Convert.ToSingle(this.txt_Rivet2OKPositionVariation.Text.Trim());
                    objSettings.Rivet2OKPositionVariationSamplintTime = Convert.ToSingle(this.txt_Rivet2OKPositionVariationSamplintTime.Text.Trim());
                    objSettings.Rivet2OKTorqueJudgment = this.chk_Rivet2OKTorqueJudgment.Checked;
                    objSettings.Rivet2OKTorque = Convert.ToSingle(this.txt_Rivet2OKTorque.Text.Trim());
                    objSettings.Rivet2OKPositionJudgment = this.chk_Rivet2OKPositionJudgment.Checked;
                    objSettings.Rivet2OKPosition = Convert.ToSingle(this.txt_Rivet2OKPosition.Text.Trim());
                    objSettings.Rivet2OverTime = Convert.ToSingle(this.txt_Rivet2OverTime.Text.Trim());
                    objSettings.Rivet2MaxSpeed = Convert.ToSingle(this.txt_Rivet2MaxSpeed.Text.Trim());
                    objSettings.Rivet3Force = Convert.ToSingle(this.txt_Rivet3Force.Text.Trim());
                    objSettings.Rivet3ForceP = Convert.ToSingle(this.txt_Rivet3ForceP.Text.Trim());
                    objSettings.Rivet3ForceI = Convert.ToSingle(this.txt_Rivet3ForceI.Text.Trim());
                    objSettings.Rivet3RotationalSpeed = Convert.ToSingle(this.txt_Rivet3RotationalSpeed.Text.Trim());
                    objSettings.Rivet3RotationalACCTime = Convert.ToSingle(this.txt_Rivet3RotationalACCTime.Text.Trim());
                    objSettings.Rivet3RotationalDECTime = Convert.ToSingle(this.txt_Rivet3RotationalDECTime.Text.Trim());
                    objSettings.Rivet3OKUpPosition = Convert.ToSingle(this.txt_Rivet3OKUpPosition.Text.Trim());
                    objSettings.Rivet3OKDownPosition = Convert.ToSingle(this.txt_Rivet3OKDownPosition.Text.Trim());
                    objSettings.Rivet3OKSpeedJudgment = this.chk_Rivet3OKSpeedJudgment.Checked;
                    objSettings.Rivet3OKSpeed = Convert.ToSingle(this.txt_Rivet3OKSpeed.Text.Trim());
                    objSettings.Rivet3OKPositionVariationJudgment = this.chk_Rivet3OKPositionVariationJudgment.Checked;
                    objSettings.Rivet3OKPositionVariation = Convert.ToSingle(this.txt_Rivet3OKPositionVariation.Text.Trim());
                    objSettings.Rivet3OKPositionVariationSamplintTime = Convert.ToSingle(this.txt_Rivet3OKPositionVariationSamplintTime.Text.Trim());
                    objSettings.Rivet3OKTorqueJudgment = this.chk_Rivet3OKTorqueJudgment.Checked;
                    objSettings.Rivet3OKTorque = Convert.ToSingle(this.txt_Rivet3OKTorque.Text.Trim());
                    objSettings.Rivet3OKPositionJudgment = this.chk_Rivet3OKPositionJudgment.Checked;
                    objSettings.Rivet3OKPosition = Convert.ToSingle(this.txt_Rivet3OKPosition.Text.Trim());
                    objSettings.Rivet3OverTime = Convert.ToSingle(this.txt_Rivet3OverTime.Text.Trim());
                    objSettings.Rivet3MaxSpeed = Convert.ToSingle(this.txt_Rivet3MaxSpeed.Text.Trim());

                    objSettings.Comment = this.txt_Comment.Text.Trim();


            }
            catch(Exception ex)
            {
                int i = 0;


            }

            //return;
            #endregion






            #region 封装紧固件对象
            Fastener objFastener = new Fastener()
            {
                FastenerName = this.txt_FastenerName.Text.Trim(),// + txt_FastenerId.Text.Trim(),
                //FastenerId = Convert.ToInt32(this.txt_FastenerId.Text.Trim()),
                Para_D = Convert.ToSingle(this.txt_Para_D.Text.Trim()),
                Para_P = Convert.ToSingle(this.txt_Para_P.Text.Trim()),
                Para_H = Convert.ToSingle(this.txt_Para_H.Text.Trim()),
                Para_L = Convert.ToSingle(this.txt_Para_L.Text.Trim()),
            };
            #endregion



            if (objFastenerService.IsFastenerNameExisted(this.txt_FastenerName.Text.Trim()))  //1存在+ txt_FastenerId.Text.Trim()
            {

                #region 调用后台数据访问方法,将更新后的对象信息存入数据库
                try
                {
                    if (objFastenerService.ModifyNewFastener(objFastener) == 1)
                    {
                        // MessageBox.Show("紧固件参数更新成功", "提示信息");
                    }
                    else
                    {
                        // MessageBox.Show("更新失败", "提示信息");
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
                #endregion

            }
            else
            {

                #region 调用后台数据访问方法,将对象信息存入数据库
                try
                {
                    if (objFastenerService.AddNewFastener(objFastener) == 1)
                    {
                        // MessageBox.Show("新紧固件添加成功", "提示信息");
                    }
                    else
                    {
                        // MessageBox.Show("添加失败", "提示信息");
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
                #endregion
            }










            #region 封装板材组合对象
            LayerStack objLayerStack = new LayerStack()
            {
                LayerStackName = this.txt_Material1.Text.Trim() + "_" + this.txt_Thickness1.Text.Trim() + "+" + this.txt_Material2.Text.Trim() + "_" + this.txt_Thickness2.Text.Trim(),//this.txt_LayerStackName.Text.Trim(),
                //LayerStackId = Convert.ToInt32(this.txt_LayerStackId.Text.Trim()),
                Material1 = Convert.ToString(this.txt_Material1.Text.Trim()),
                Material2 = Convert.ToString(this.txt_Material2.Text.Trim()),
                Thickness1 = Convert.ToSingle(this.txt_Thickness1.Text.Trim()),
                Thickness2 = Convert.ToSingle(this.txt_Thickness2.Text.Trim())
            };
            #endregion

            txt_LayerStackName.Text = objLayerStack.LayerStackName;
            //验证编号是否在数据库内已经存在
            if (objLayerStackService.IsNewLayerStackNameExisted(objLayerStack.LayerStackName))
            {

                #region 调用后台数据访问方法,将对象信息存入数据库
                try
                {
                    if (objLayerStackService.ModifyNewLayerStackName(objLayerStack) == 1)
                    {
                        //MessageBox.Show("板材组合参数更新成功", "提示信息");
                    }
                    else
                    {
                        //MessageBox.Show("更新失败", "提示信息");
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
                #endregion
            }
            else
            {
                #region 调用后台数据访问方法,将对象信息存入数据库
                try
                {
                    if (objLayerStackService.AddNewLayerStackName(objLayerStack) == 1)
                    {
                        //MessageBox.Show("新板材组合添加成功", "提示信息");
                    }
                    else
                    {
                        // MessageBox.Show("添加失败", "提示信息");
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
                #endregion
            }






















            #region 调用后台数据访问方法,将对象信息存入数据库
            try
            {
                if (objSettingsService.AddSettings(objSettings) == 1)
                {
                    MessageBox.Show("新工艺规范添加成功", "提示信息");
                }
                else
                {
                    MessageBox.Show("添加失败", "提示信息");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            #endregion











            UpdateDGV();
            CommonMethods.FastenerList = objFastenerService.GetAll();
            CommonMethods.LayerStackList = objLayerStackService.GetAll();
            CommonMethods.SettingsList = objSettingsService.GetAll();
            this.cbo_Fastener.SelectedValue = objSettings.FastenerName.ToString();// + objSettings.FastenerId.ToString();

            for (int i = 0; i < dgv_data.Rows.Count; i++)
            {
                dgv_data.Rows[i].Selected = false;
            }

            for (int i = 0; i < dgv_data.Rows.Count; i++)
            {
                if (dgv_data.Rows[i].Cells[0].Value.ToString() == objSettings.SettingsId.ToString())
                {
                    dgv_data.Rows[i].Selected = true;
                    UpdateText_Insert(objSettings);
                }
            }


            this.dgv_data.Enabled = true;//点击确定,编辑完成
            this.btn_Delete.Enabled = true;
            this.btn_New.Enabled = true;
            this.btn_Edit.Enabled = true;

            this.btn_SaveEdit.Visible = false;
            this.btn_SaveNew.Visible = false;
            this.btn_Cancel.Visible = false;
        }

        #region 被选中行RowEnter事件,得到被选中行的索引值,和SettingsList集合的索引一一对应
        private void dgv_data_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
            //DGV初始加载和选择非当前行触发RowEnter事件,该事件结束后CurrentRow属性才会被更新
            objSettings = new Settings();
            int i = e.RowIndex;//索引值按照由小到大,集合由数据库默认按照主键增序存入,索引一一对应
            objSettings = CommonMethods.SettingsList[i];
            //objSettings.EidtId = Convert.ToInt32(this.dgv_data.Rows[i].Cells["SettingsId"].Value);
            UpdateText();
        }
        #endregion

        #region UpdateDGV()从数据库读取信息存入集合,重新绑定刷新DataGridView
        private void UpdateDGV()//从数据库读取集合信息
        {
            CommonMethods.SettingsList = objSettingsService.GetAll();
            this.dgv_data.DataSource = null;
            this.dgv_data.DataSource = CommonMethods.SettingsList;
        }
        #endregion

        #region UpdateDGV_ByName()从数据库模糊查询读取信息存入集合,重新绑定刷新DataGridView
        private void UpdateDGV_ByName(string vname)//从数据库读取集合信息
        {
            CommonMethods.SettingsList = objSettingsService.GetSearch(vname);
            this.dgv_data.DataSource = null;
            this.dgv_data.DataSource = CommonMethods.SettingsList;
        }

        #endregion

        #region UpdateText()更新文本等显示
        /// <summary>
        /// 更新TextBox等显示
        /// </summary>
        private void UpdateText()//TextBox赋值并置为不可编辑
        {
            this.txt_MultistageId.Text = objSettings.MultistageId.ToString();
            this.txt_SettingsName.Text = objSettings.SettingsName;
            this.txt_SettingsId.Text = objSettings.SettingsId.ToString();
            this.txt_Rivet1OKUpPosition.Text = objSettings.TargetVelo1.ToString();
            this.txt_Rivet1OKDownPosition.Text = objSettings.TargetVelo2.ToString();
            this.txt_Rivet1OKSpeedJudgment.Text = objSettings.TargetVelo3.ToString();
            this.txt_Rivet1OKPositionVariation.Text = objSettings.TargetVelo4.ToString();
            this.txt_Rivet1OKPositionVariationSamplintTime.Text = objSettings.TargetVelo5.ToString();
            this.txt_Rivet1OKTorqueJudgment.Text = objSettings.RotationalSpeed1.ToString();
            this.txt_Rivet1OKTorque.Text = objSettings.RotationalSpeed2.ToString();
            this.txt_Rivet1OKPositionJudgment.Text = objSettings.RotationalSpeed3.ToString();
            this.txt_Rivet1OKPosition.Text = objSettings.RotationalSpeed4.ToString();
            this.txt_Rivet1OverTime.Text = objSettings.RotationalSpeed5.ToString();
            this.txt_Rivet1Force.Text = objSettings.TargetPos1.ToString();
            this.txt_Rivet1ForceP.Text = objSettings.TargetPos2.ToString();
            this.txt_Rivet1RotationalSpeed.Text = objSettings.TargetPos3.ToString();
            this.txt_Rivet1RotationalACCTime.Text = objSettings.TargetPos4.ToString();
            this.txt_Rivet1RotationalDECTime.Text = objSettings.TargetPos5.ToString();


            this.txt_TargetDownforce1.Text = objSettings.TargetDownforce1.ToString();
            this.txt_MaxFeedingTime.Text = objSettings.MaxFeedingTime.ToString();
            this.txt_MaxPrePressingTime.Text = objSettings.MaxPrePressingTime.ToString();
            this.txt_MaxRivetingTime.Text = objSettings.MaxRivetingTime.ToString();
            this.txt_UPLimitPos.Text = objSettings.UPLimitPos.ToString();
            this.txt_StopTorque.Text = objSettings.StopTorque.ToString();
            this.txt_Constant_Downforce1.Text = objSettings.Constant_Downforce1.ToString();
            this.txt_PressTime.Text = objSettings.PressTime.ToString();
            this.txt_TargetVelo6.Text = objSettings.TargetVelo6.ToString();
            this.txt_TargetPos6.Text = objSettings.TargetPos6.ToString();
            this.txt_FeedPos1.Text = objSettings.FeedPos1.ToString();
            this.txt_FeedVelo1.Text = objSettings.FeedVelo1.ToString();
            this.txt_FSR_FeedPos1.Text = objSettings.FSR_FeedPos1.ToString();
            this.txt_FeedVelo2.Text = objSettings.FeedVelo2.ToString();
            this.txt_FSR_ContactPos1.Text = objSettings.FSR_ContactPos1.ToString();
            this.txt_UpperPos1.Text = objSettings.UpperPos1.ToString();
            this.txt_LowerPos1.Text = objSettings.LowerPos1.ToString();
            this.txt_StopPressure1.Text = objSettings.StopPressure1.ToString();
            this.txt_Pre_Downforce1.Text = objSettings.Pre_Downforce1.ToString();
            this.txt_PrePressureSetValue.Text = objSettings.PrePressureSetValue.ToString();
            this.txt_FastenerName.Text = objSettings.FastenerName.ToString();
            //this.txt_FastenerId.Text = objSettings.FastenerId.ToString().PadLeft(2, '0');
            this.txt_Para_D.Text = objSettings.Para_D.ToString();
            this.txt_Para_P.Text = objSettings.Para_P.ToString();
            this.txt_Para_H.Text = objSettings.Para_H.ToString();
            this.txt_Para_L.Text = objSettings.Para_L.ToString();
            this.txt_LayerStackName.Text = objSettings.Material1.ToString().Trim() + "_" + objSettings.Thickness1.ToString().Trim() + "+" + objSettings.Material2.ToString().Trim() + "_" + objSettings.Thickness2.ToString().Trim();//    objSettings.LayerStackName.ToString();
            //this.txt_LayerStackId.Text = objSettings.LayerStackId.ToString();
            this.txt_Material1.Text = objSettings.Material1.ToString();
            this.txt_Thickness1.Text = objSettings.Thickness1.ToString();
            this.txt_Material2.Text = objSettings.Material2.ToString();
            this.txt_Thickness2.Text = objSettings.Thickness2.ToString();

            this.btn_PressConfirmed.Enabled = false;
            this.btn_StopTorqueConfirmed.Enabled = false;
            //if(this.btn_PressConfirmed.colo)
            if (objSettings.PressConfirmed == true)
                this.btn_PressConfirmed.BackColor = Color.FromArgb(60, 179, 113);
            else
                this.btn_PressConfirmed.BackColor = Color.FromArgb(255, 255, 255);
            //this.btn_PressConfirmed.Enabled = objSettings.PressConfirmed;

            if (objSettings.StopTorqueConfirmed == true)
                this.btn_StopTorqueConfirmed.BackColor = Color.FromArgb(60, 179, 113);
            else
                this.btn_StopTorqueConfirmed.BackColor = Color.FromArgb(255, 255, 255);


            this.txt_PrePressHighPosition.Text = objSettings.PrePressHighPosition.ToString();
            this.txt_PrePressHighSpeed.Text = objSettings.PrePressHighSpeed.ToString();
            this.txt_PrePressLowPosition.Text = objSettings.PrePressLowPosition.ToString();
            this.txt_PrePressLowSpeed.Text = objSettings.PrePressLowSpeed.ToString();
            this.txt_PrePressTouchForce.Text = objSettings.PrePressTouchForce.ToString();
            this.txt_PrePressForce.Text = objSettings.PrePressForce.ToString();
            this.txt_Rivet1Force.Text = objSettings.Rivet1Force.ToString();
            this.txt_Rivet1ForceP.Text = objSettings.Rivet1ForceP.ToString();
            this.txt_Rivet1ForceI.Text = objSettings.Rivet1ForceI.ToString();
            this.txt_Rivet1RotationalSpeed.Text = objSettings.Rivet1RotationalSpeed.ToString();
            this.txt_Rivet1RotationalACCTime.Text = objSettings.Rivet1RotationalACCTime.ToString();
            this.txt_Rivet1RotationalDECTime.Text = objSettings.Rivet1RotationalDECTime.ToString();
            this.txt_Rivet1OKUpPosition.Text = objSettings.Rivet1OKUpPosition.ToString();
            this.txt_Rivet1OKDownPosition.Text = objSettings.Rivet1OKDownPosition.ToString();
            this.chk_Rivet1OKSpeedJudgment.Checked = objSettings.Rivet1OKSpeedJudgment;
            this.txt_Rivet1OKSpeed.Text = objSettings.Rivet1OKSpeed.ToString();
            this.chk_Rivet1OKPositionVariationJudgment.Checked = objSettings.Rivet1OKPositionVariationJudgment;
            this.txt_Rivet1OKPositionVariation.Text = objSettings.Rivet1OKPositionVariation.ToString();
            this.txt_Rivet1OKPositionVariationSamplintTime.Text = objSettings.Rivet1OKPositionVariationSamplintTime.ToString();
            this.chk_Rivet1OKTorqueJudgment.Checked = objSettings.Rivet1OKTorqueJudgment;
            this.txt_Rivet1OKTorque.Text = objSettings.Rivet1OKTorque.ToString();
            this.chk_Rivet1OKPositionJudgment.Checked = objSettings.Rivet1OKPositionJudgment;
            this.txt_Rivet1OKPosition.Text = objSettings.Rivet1OKPosition.ToString();
            this.txt_Rivet1OverTime.Text = objSettings.Rivet1OverTime.ToString();
            this.txt_Rivet1MaxSpeed.Text = objSettings.Rivet1MaxSpeed.ToString();
            this.txt_Rivet2Force.Text = objSettings.Rivet2Force.ToString();
            this.txt_Rivet2ForceP.Text = objSettings.Rivet2ForceP.ToString();
            this.txt_Rivet2ForceI.Text = objSettings.Rivet2ForceI.ToString();
            this.txt_Rivet2RotationalSpeed.Text = objSettings.Rivet2RotationalSpeed.ToString();
            this.txt_Rivet2RotationalACCTime.Text = objSettings.Rivet2RotationalACCTime.ToString();
            this.txt_Rivet2RotationalDECTime.Text = objSettings.Rivet2RotationalDECTime.ToString();
            this.txt_Rivet2OKUpPosition.Text = objSettings.Rivet2OKUpPosition.ToString();
            this.txt_Rivet2OKDownPosition.Text = objSettings.Rivet2OKDownPosition.ToString();
            this.chk_Rivet2OKSpeedJudgment.Checked = objSettings.Rivet2OKSpeedJudgment;
            this.txt_Rivet2OKSpeed.Text = objSettings.Rivet2OKSpeed.ToString();
            this.chk_Rivet2OKPositionVariationJudgment.Checked = objSettings.Rivet2OKPositionVariationJudgment;
            this.txt_Rivet2OKPositionVariation.Text = objSettings.Rivet2OKPositionVariation.ToString();
            this.txt_Rivet2OKPositionVariationSamplintTime.Text = objSettings.Rivet2OKPositionVariationSamplintTime.ToString();
            this.chk_Rivet2OKTorqueJudgment.Checked = objSettings.Rivet2OKTorqueJudgment;
            this.txt_Rivet2OKTorque.Text = objSettings.Rivet2OKTorque.ToString();
            this.chk_Rivet2OKPositionJudgment.Checked = objSettings.Rivet2OKPositionJudgment;
            this.txt_Rivet2OKPosition.Text = objSettings.Rivet2OKPosition.ToString();
            this.txt_Rivet2OverTime.Text = objSettings.Rivet2OverTime.ToString();
            this.txt_Rivet2MaxSpeed.Text = objSettings.Rivet2MaxSpeed.ToString();
            this.txt_Rivet3Force.Text = objSettings.Rivet3Force.ToString();
            this.txt_Rivet3ForceP.Text = objSettings.Rivet3ForceP.ToString();
            this.txt_Rivet3ForceI.Text = objSettings.Rivet3ForceI.ToString();
            this.txt_Rivet3RotationalSpeed.Text = objSettings.Rivet3RotationalSpeed.ToString();
            this.txt_Rivet3RotationalACCTime.Text = objSettings.Rivet3RotationalACCTime.ToString();
            this.txt_Rivet3RotationalDECTime.Text = objSettings.Rivet3RotationalDECTime.ToString();
            this.txt_Rivet3OKUpPosition.Text = objSettings.Rivet3OKUpPosition.ToString();
            this.txt_Rivet3OKDownPosition.Text = objSettings.Rivet3OKDownPosition.ToString();
            this.chk_Rivet3OKSpeedJudgment.Checked = objSettings.Rivet3OKSpeedJudgment;
            this.txt_Rivet3OKSpeed.Text = objSettings.Rivet3OKSpeed.ToString();
            this.chk_Rivet3OKPositionVariationJudgment.Checked = objSettings.Rivet3OKPositionVariationJudgment;
            this.txt_Rivet3OKPositionVariation.Text = objSettings.Rivet3OKPositionVariation.ToString();
            this.txt_Rivet3OKPositionVariationSamplintTime.Text = objSettings.Rivet3OKPositionVariationSamplintTime.ToString();
            this.chk_Rivet3OKTorqueJudgment.Checked = objSettings.Rivet3OKTorqueJudgment;
            this.txt_Rivet3OKTorque.Text = objSettings.Rivet3OKTorque.ToString();
            this.chk_Rivet3OKPositionJudgment.Checked = objSettings.Rivet3OKPositionJudgment;
            this.txt_Rivet3OKPosition.Text = objSettings.Rivet3OKPosition.ToString();
            this.txt_Rivet3OverTime.Text = objSettings.Rivet3OverTime.ToString();
            this.txt_Rivet3MaxSpeed.Text = objSettings.Rivet3MaxSpeed.ToString();
             
            this.txt_Comment.Text = objSettings.Comment.ToString();



            this.cbo_Fastener.DataSource = null;
            this.cbo_Fastener.DisplayMember = "FastenerName";
            this.cbo_Fastener.ValueMember = "FastenerName";
            this.cbo_Fastener.DataSource = CommonMethods.FastenerList;

            this.cbo_LayerStack.DataSource = null;
            this.cbo_LayerStack.DisplayMember = "LayerStackName";
            this.cbo_LayerStack.ValueMember = "LayerStackName";
            this.cbo_LayerStack.DataSource = CommonMethods.LayerStackList;

            this.cbo_Fastener.SelectedValue = objSettings.FastenerName.ToString();// + objSettings.FastenerId.ToString();
            this.cbo_LayerStack.SelectedValue = objSettings.Material1 + "_" + objSettings.Thickness1 + "+" + objSettings.Material2 + "_" + objSettings.Thickness2;
            this.cbo_Fastener.Enabled = false;
            this.cbo_LayerStack.Enabled = false;
            this.txt_SettingsName.Enabled = false;
            this.txt_SettingsId.Enabled = false;

            foreach (TabPage page in tabControl1.TabPages)
            {
                foreach (Control item in page.Controls)
                {
                    if (item is TextBox)
                    {
                        item.Enabled = false;
                    }
                }


                foreach (Control item in page.Controls)
                {
                    if (item is CheckBox)
                    {
                        item.Enabled = false;
                    }
                }
            }
        }

        #endregion

        private void dgv_data_CellMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            this.btn_Edit_Click(null, null);
        }

        private void Btn_Search_Click(object sender, EventArgs e)
        {
            UpdateDGV_ByName(txt_SearchName.Text);
        }



        public static void UpdateParameter(string VariableName, string val)
        {
            Beckhoff_PLC.IsWriting = true;
            if (VariableName.IndexOf("Main_Sys.Mach") != -1) VariableName = VariableName.TrimStart('.');
            else if (VariableName.IndexOf("I_") != -1) VariableName = VariableName.TrimStart('.');
            else if (VariableName.IndexOf("Main_Sys.JOGSetVelo") != -1) VariableName = VariableName.TrimStart('.');
            else VariableName = VariableName;
            if (Beckhoff_PLC.WriteVariable(VariableName, float.Parse(val.Trim())))//将每个变量的变量名传来,在Tag中
            {
            }
            else
            {
                //MessageBox.Show("修改失败！", "修改提示");
            }
            Beckhoff_PLC.IsWriting = false;
        }


        public static void UpdateParameter_int(string VariableName, string val)
        {
            Beckhoff_PLC.IsWriting = true;
            if (VariableName.IndexOf("Main_Sys.Mach") != -1) VariableName = VariableName.TrimStart('.');
            else if (VariableName.IndexOf("I_") != -1) VariableName = VariableName.TrimStart('.');
            else if (VariableName.IndexOf("Main_Sys.JOGSetVelo") != -1) VariableName = VariableName.TrimStart('.');
            else VariableName = "_03_HMI" + VariableName;
            if (Beckhoff_PLC.WriteVariable(VariableName, ushort.Parse(val.Trim())))//将每个变量的变量名传来,在Tag中
            {
            }
            else
            {
                //MessageBox.Show("修改失败！", "修改提示");
            }
            Beckhoff_PLC.IsWriting = false;
        }


        private void button1_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("是否下发参数", "退出", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.Cancel)
            {
            }
            else
            {
                 
                foreach (TabPage page in tabControl1.TabPages)
                {
                    foreach (Control item in page.Controls)
                    {
                        if (item is CheckBox)
                        {
                            if (item.Tag.ToString().IndexOf(".") != -1 )   
                            {
                               
                                Beckhoff_PLC.IsWriting = true;
                                if (((CheckBox)item).Checked == false)
                                {
                                    Beckhoff_PLC.WriteVariable(item.Tag.ToString(), false);
                                }
                                if (((CheckBox)item).Checked == true)
                                {
                                    Beckhoff_PLC.WriteVariable(item.Tag.ToString(), true);
                                }
                                Beckhoff_PLC.IsWriting = false;

                            }

                        }

                        if (item is TextBox)
                        {
                            if (item.Tag != null && item.Tag.ToString() != "" && item.Tag.ToString().IndexOf(".") != -1)
                            {
                                Beckhoff_PLC.IsWriting = true;
                                Thread.Sleep(50);
                                if (CommonMethods.CurrentType[item.Tag.ToString()] == "float")
                                {
                                    if (Beckhoff_PLC.WriteVariable(item.Tag.ToString(), float.Parse(item.Text.ToString().Trim())))//将每个变量的变量名传来,在Tag中
                                    {
                                       
                                    }
                                    else
                                    {
                                      
                                    }
                                }

                                if (CommonMethods.CurrentType[item.Tag.ToString()] == "ushort")
                                {
                                    if (Beckhoff_PLC.WriteVariable(item.Tag.ToString(), ushort.Parse(item.Text.ToString().Trim())))//将每个变量的变量名传来,在Tag中
                                    {
                                      
                                    }
                                    else
                                    {
                                      
                                    }
                                }

                                if (CommonMethods.CurrentType[item.Tag.ToString()] == "int")
                                {
                                    if (Beckhoff_PLC.WriteVariable(item.Tag.ToString(), Int16.Parse(item.Text.ToString().Trim())))//将每个变量的变量名传来,在Tag中
                                    {
                                        
                                    }
                                    else
                                    {
                                      
                                    }
                                }
                                if (CommonMethods.CurrentType[item.Tag.ToString()] == "UInt32")
                                {
                                    if (Beckhoff_PLC.WriteVariable(item.Tag.ToString(), UInt32.Parse(item.Text.ToString().Trim())))//将每个变量的变量名传来,在Tag中
                                    {
                                      
                                    }
                                    else
                                    {
                                       
                                    }
                                }
                                Beckhoff_PLC.IsWriting = false;
                            }
 
                        }
                    }
                }

                for (int i = 0; i < dgv_data.Rows.Count; i++)
                {
                    if (dgv_data.Rows[i].Selected == true)
                    {

                 

                        //CommonMethods.SettingsIndexBak = i + 1;
                        FrmView.RunSettingsid = dgv_data.Rows[i].Cells[0].Value.ToString();// CommonMethods.SettingsIndexBak.ToString();
                        CommonMethods.CurrentValue["_03_HMI.settingsId"] = FrmView.RunSettingsid;
                    }
                }
                  DataSync.SavePowerOffValueToCSV(System.Environment.CurrentDirectory + "\\ConfigFile\\Cache.csv");
                MessageBox.Show("参数下发完成");
            }
        }

        private void Btn_Search_Click_1(object sender, EventArgs e)
        {
            UpdateDGV_ByName(txt_SearchName.Text);
            //var resultList
            //CommonMethods.LayerStackTmpList = CommonMethods.LayerStackList.Where(x => x.LayerStackName == "al_1.0+steel_2.0").ToList();  
        }

        private void tb_Click(object sender, EventArgs e)
        {
            TextBox tb_temp = (TextBox)sender;
            FrmKeyPad.input = tb_temp.Text;
            // 获取 TextBox 的屏幕位置
            Point textboxLocation = tb_temp.PointToScreen(Point.Empty);
            // 创建一个新的窗体
            FrmKeyPad frmSoftKey = new FrmKeyPad(tb_temp.Text);
            // 计算窗体的位置
            //textboxLocation.X = 1;
            //textboxLocation.Y = 1;
            int popupX = textboxLocation.X;
            int popupY = textboxLocation.Y;
            // 设置窗体的位置
            frmSoftKey.Location = new Point(popupX, popupY);
            // 显示窗体
            frmSoftKey.ShowDialog();
            //MessageBox.Show("显示键盘之后");
            //MessageBox.Show(FrmSoftKey.input);
            if (FrmKeyPad.confirm)
            {
                tb_temp.Text = FrmKeyPad.input;
                tb_temp.SelectionStart = tb_temp.Text.Length;
                tb_temp.SelectionLength = 0;
                tb_temp.Focus();
            }
        }



        private void tb_PadNum_Click(object sender, EventArgs e)
        {
            TextBox tb_temp = (TextBox)sender;
            FrmKeyPadNum.input = tb_temp.Text;
            // 获取 TextBox 的屏幕位置
            Point textboxLocation = tb_temp.PointToScreen(Point.Empty);
            // 创建一个新的窗体
            FrmKeyPadNum frmSoftKey = new FrmKeyPadNum(tb_temp.Text);
            // 计算窗体的位置
            //textboxLocation.X = 1;
            //textboxLocation.Y = 1;
            int popupX = textboxLocation.X;
            int popupY = textboxLocation.Y;
            // 设置窗体的位置
            frmSoftKey.Location = new Point(popupX, popupY);
            // 显示窗体
            frmSoftKey.ShowDialog();
            //MessageBox.Show("显示键盘之后");
            //MessageBox.Show(FrmSoftKey.input);
            if (FrmKeyPadNum.confirm)
            {
                tb_temp.Text = FrmKeyPadNum.input;
                tb_temp.SelectionStart = tb_temp.Text.Length;
                tb_temp.SelectionLength = 0;
                tb_temp.Focus();
            }
        }

        private void MainPanel_Paint(object sender, PaintEventArgs e)
        {

        }

        private void tabPage5_Click(object sender, EventArgs e)
        {

        }

        private void txt_MultistageId_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!(Char.IsNumber(e.KeyChar)) && e.KeyChar != (char)8)
                e.Handled = true;
        }

        private void txt_MultistageId_TextChanged(object sender, EventArgs e)
        {
            if (txt_MultistageId.Text == "")
                txt_MultistageId.Text = 1.ToString();
            int number = int.Parse(txt_MultistageId.Text);
            txt_MultistageId.Text = number.ToString();
            if (number >= 1 && number <= 5)
            {
                // return;
                txt_MultistageId.Text = number.ToString();
            }
            else if (number < 1)
            {
                txt_MultistageId.Text = "1";
                number = 1;
            }
            else if (number > 5)
            {
                txt_MultistageId.Text = "5";
                number = 5;
            }

            //switch (number)
            //{
            //    case 1:
            //        txt_Rivet1ForceP.Text = "0";
            //        txt_Rivet1OKDownPosition.Text = "0";
            //        txt_Rivet1OKTorque.Text = "0";
            //        txt_Rivet1RotationalSpeed.Text = "0";
            //        txt_Rivet1OKSpeedJudgment.Text = "0";
            //        txt_Rivet1OKPositionJudgment.Text = "0";
            //        txt_Rivet1RotationalACCTime.Text = "0";
            //        txt_Rivet1OKPositionVariation.Text = "0";
            //        txt_Rivet1OKPosition.Text = "0";
            //        txt_Rivet1RotationalDECTime.Text = "0";
            //        txt_Rivet1OKPositionVariationSamplintTime.Text = "0";
            //        txt_Rivet1OverTime.Text = "0";
            //        break;
            //    case 2:
            //        txt_Rivet1RotationalSpeed.Text = "0";
            //        txt_Rivet1OKSpeedJudgment.Text = "0";
            //        txt_Rivet1OKPositionJudgment.Text = "0";
            //        txt_Rivet1RotationalACCTime.Text = "0";
            //        txt_Rivet1OKPositionVariation.Text = "0";
            //        txt_Rivet1OKPosition.Text = "0";
            //        txt_Rivet1RotationalDECTime.Text = "0";
            //        txt_Rivet1OKPositionVariationSamplintTime.Text = "0";
            //        txt_Rivet1OverTime.Text = "0";
            //        break;
            //    case 3:
            //        txt_Rivet1RotationalACCTime.Text = "0";
            //        txt_Rivet1OKPositionVariation.Text = "0";
            //        txt_Rivet1OKPosition.Text = "0";
            //        txt_Rivet1RotationalDECTime.Text = "0";
            //        txt_Rivet1OKPositionVariationSamplintTime.Text = "0";
            //        txt_Rivet1OverTime.Text = "0";
            //        break;
            //    case 4:
            //        txt_Rivet1RotationalDECTime.Text = "0";
            //        txt_Rivet1OKPositionVariationSamplintTime.Text = "0";
            //        txt_Rivet1OverTime.Text = "0";
            //        break;
            //    case 5:

            //        break;
            //    default:
            //        break;
            //}
        }

        private void btn_PressConfirmed_Click(object sender, EventArgs e)
        {
            //string str = "Update ";
            if (this.btn_PressConfirmed.BackColor == Color.FromArgb(60, 179, 113))
            {
                this.btn_PressConfirmed.BackColor = Color.FromArgb(255, 255, 255);
            }
            else
            {
                this.btn_PressConfirmed.BackColor = Color.FromArgb(60, 179, 113);
            }

            //Beckhoff_PLC.IsWriting = true;
            //if (CommonMethods.CurrentValue["PressConfirmed"] == "False")
            //{
            //    Beckhoff_PLC.WriteVariable(".PressConfirmed", true);
            //}
            //if (CommonMethods.CurrentValue["PressConfirmed"] == "True")
            //{
            //    Beckhoff_PLC.WriteVariable(".PressConfirmed", false);
            //}
            //Beckhoff_PLC.IsWriting = false;

        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (this.btn_StopTorqueConfirmed.BackColor == Color.FromArgb(60, 179, 113))
            {
                this.btn_StopTorqueConfirmed.BackColor = Color.FromArgb(255, 255, 255);
            }
            else
            {
                this.btn_StopTorqueConfirmed.BackColor = Color.FromArgb(60, 179, 113);
            }

            //Beckhoff_PLC.IsWriting = true;
            //if (CommonMethods.CurrentValue["StopTorqueConfirmed"] == "False")
            //{
            //    Beckhoff_PLC.WriteVariable(".StopTorqueConfirmed", true);
            //}
            //if (CommonMethods.CurrentValue["StopTorqueConfirmed"] == "True")
            //{
            //    Beckhoff_PLC.WriteVariable(".StopTorqueConfirmed", false);
            //}
            //Beckhoff_PLC.IsWriting = false;

        }

        private void cbo_Fastener_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                CommonMethods.FastenerTmpList = CommonMethods.FastenerList.Where(x => x.FastenerName == cbo_Fastener.SelectedValue).ToList();
                //this.txt_FastenerId.Text = CommonMethods.FastenerTmpList[0].FastenerId.ToString().PadLeft(2, '0');
                this.txt_FastenerName.Text = CommonMethods.FastenerTmpList[0].FastenerName;
                this.txt_Para_D.Text = CommonMethods.FastenerTmpList[0].Para_D.ToString();
                this.txt_Para_P.Text = CommonMethods.FastenerTmpList[0].Para_P.ToString();
                this.txt_Para_H.Text = CommonMethods.FastenerTmpList[0].Para_H.ToString();
                this.txt_Para_L.Text = CommonMethods.FastenerTmpList[0].Para_L.ToString();
            }
            catch
            {

            }


        }

        private void cbo_LayerStack_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                CommonMethods.LayerStackTmpList = CommonMethods.LayerStackList.Where(x => x.LayerStackName == cbo_LayerStack.SelectedValue).ToList();
                this.txt_LayerStackName.Text = CommonMethods.LayerStackTmpList[0].LayerStackName;
                this.txt_Material1.Text = CommonMethods.LayerStackTmpList[0].Material1;
                this.txt_Thickness1.Text = CommonMethods.LayerStackTmpList[0].Thickness1.ToString();
                this.txt_Material2.Text = CommonMethods.LayerStackTmpList[0].Material2;
                this.txt_Thickness2.Text = CommonMethods.LayerStackTmpList[0].Thickness2.ToString();
            }
            catch
            {

            }
        }

        private void TopPanel_Paint(object sender, PaintEventArgs e)
        {

        }

        private void button2_Click_1(object sender, EventArgs e)
        {

            if (MessageBox.Show("是否导入工艺文件", "退出", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.Cancel)
            {

            }
            else
            {

                OpenFileDialog openFileDialog = new OpenFileDialog();
                openFileDialog.InitialDirectory = @"c:\"; // 设置初始目录
                openFileDialog.Filter = "csv files (*.csv)|*.csv|All files (*.*)|*.*"; // 设置文件过滤器
                openFileDialog.FilterIndex = 2;
                openFileDialog.RestoreDirectory = true;

                if (openFileDialog.ShowDialog() == DialogResult.OK) // 显示对话框
                {
                    // 获取选中文件的路径
                    string filePath = openFileDialog.FileName;
                    Console.WriteLine($"Selected file: {filePath}");

                    SettingsService.importFile(filePath);
                }

            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("是否导出工艺文件", "退出", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.Cancel)
            {

            }
            else
            {
                DataSet ds = new DataSet();
                ds = SettingsService.exportFile();
                Toexcel.DataTableProcessParametersToCSV(ds.Tables[0], @"c:\工艺参数.csv");
          
            }
        }

        private void label89_Click(object sender, EventArgs e)
        {

        }

        private void ModifyTxt_UserControlClick(object sender, EventArgs e)
        {

        }

        private void splitContainer2_Panel1_Paint(object sender, PaintEventArgs e)
        {

        }

        private void splitContainer2_Panel2_Paint(object sender, PaintEventArgs e)
        {

        }

 
    }
}
